为什么 mongo 没有 return 所有字段?
Why mongo does not return all the fields?
我有一个以位置和名称作为字段的集合。
我用猫鼬为名字创建了索引,如下所示,
eventSchema.index({name: 'text'});
当我在 robomongo 上 运行 时,它 returns 所有 12 个字段,
db.getCollection('_event').find({"location.country":"United States"})
但是当我在 robomongo 上 运行 这个时,它 returns 值只有两个字段,id 和 location,
db.getCollection('_event').find({$text: {$search: "2017 Honda"}},{"location.country":"United States"})
这是因为您放错了其他查询表达式,您将其指定为投影,因此您得到的投影有两个字段:
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
表达式,也可以显式指定为
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
});
我有一个以位置和名称作为字段的集合。
我用猫鼬为名字创建了索引,如下所示,
eventSchema.index({name: 'text'});
当我在 robomongo 上 运行 时,它 returns 所有 12 个字段,
db.getCollection('_event').find({"location.country":"United States"})
但是当我在 robomongo 上 运行 这个时,它 returns 值只有两个字段,id 和 location,
db.getCollection('_event').find({$text: {$search: "2017 Honda"}},{"location.country":"United States"})
这是因为您放错了其他查询表达式,您将其指定为投影,因此您得到的投影有两个字段:
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
表达式,也可以显式指定为
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
});