我这里有一个建筑对象,里面有一个地板对象数组。
投影时,我的目标是在相应地匹配元素后返回或计算建筑对象内的地板对象的数量。代码如下:
对象:
type Floor struct {
// Binary JSON Identity
ID bson.ObjectId `bson:"_id,omitempty"`
// App-level Identity
FloorUUID string `bson:"f"`
// Floor Info
FloorNumber int `bson:"l"`
// Units
FloorUnits []string `bson:"u"`
// Statistics
Created time.Time `bson:"y"`
}
type Building struct {
// Binary JSON Identity
ID bson.ObjectId `bson:"_id,omitempty"`
// App-level Identity
BldgUUID string `bson:"b"`
// Address Info
BldgNumber string `bson:"i"` // Street Number
BldgStreet string `bson:"s"` // Street
BldgCity string `bson:"c"` // City
BldgState string `bson:"t"` // State
BldgCountry string `bson:"x"` // Country
// Building Info
BldgName string `bson:"w"`
BldgOwner string `bson:"o"`
BldgMaxTenant int `bson:"m"`
BldgNumTenant int `bson:"n"`
// Floors
BldgFloors []Floor `bson:"p"`
// Statistics
Created time.Time `bson:"z"`
}
代码:
func InsertFloor(database *mgo.Database, bldg_uuid string, fnum int) error {
fmt.Println(bldg_uuid)
fmt.Println(fnum) // Floor Number
var result Floor // result := Floor{}
database.C("buildings").Find(bson.M{"b": bldg_uuid}).Select(
bson.M{"p": bson.M{"$elemMatch": bson.M{"l": fnum}}}).One(&result)
fmt.Printf("AHA %s", result)
return errors.New("x")
}
事实证明,无论我如何尝试,查询都会返回建筑对象,而不是楼层对象?为了让查询获取和计算楼层而不是建筑物,我需要进行哪些更改?
这样做是为了在插入之前检查建筑物内的楼层是否已经存在。如果有更好的方法,那么我会用更好的方法替换我的方法!
蝴蝶刀刀
http://api.douban.com/v2/movie/in_theaters?返回值104
如何查看mongodb的collection结构?
SQL Server投影
图片的投影效果
相关分类