为什么mongo不能返回所有的字段?
我有一个位置和名称作为字段的集合。
我用下面的mongoose创build了名字索引,
eventSchema.index({name: 'text'});
当我运行这个robomongo,它返回所有12个字段,
db.getCollection('_event').find({"location.country":"United States"})
但是当我运行这个robomongo,它只返回值与两个字段,ID和位置,
db.getCollection('_event').find({$text: {$search: "2017 Honda"}},{"location.country":"United States"})
这是因为您错误地放置了其他查询expression式,所以将其指定为投影,因此您将得到带有两个字段的投影:
db.getCollection('_event').find( {$text: {$search: "2017 Honda"}}, // <-- query part {"location.country":"United States"} // <-- projection part )
您需要将其移动到查询对象中,如下所示:
db.getCollection("_event").find( { "$text": { "$search": "2017 Honda" }, "location.country": "United States" } )
这是一个隐含的$and
expression式,也可以显式指定为
db.getCollection("_event").find( { "$and": [ { "$text": { "$search": "2017 Honda" } }, { "location.country": "United States" } ] } )
db.getCollection('_event').find({ $text: {$search: "2017 Honda"}, "location.country":"United States" }, { "location":1, "_id":0 // -- here we specify required fields. 1 is showing, 0 is not showing });