db.test.find();
{
_id: ObjectId('55e99a2190df7a911d6ed1f1'),
list: [
{
"aa": 10,
"data": "test"
},
{
"aa": 1,
"data": "test"
}
]
}
db.test.find({_id: ObjectId('55e99a2190df7a911d6ed1f1'), list: {'$elemMatch': {'aa': 1}}})
预期结果:
{_id: ObjectId('55e99a2190df7a911d6ed1f1'),
list: {
"aa": 1,
"data": "test"
}
Query matching embedded documents, you can also query directly, such as:
Corresponding to your above query implementation:
db.test.find({_id: ObjectId('55e99a2190df7a911d6ed1f1'), list: {'$elemMatch': {'aa': 1}}},{"list.$":1}).pretty()
First of all, if you use mongodb, you have to understand that mongodb is nosql
Then, the mongo collection stores documents
Finally, you have to know that the value returned by find must be a collection of documents, which is an array
So what you said is basically impossible to achieve
Just fill in the query conditions you need in find. For more details, please check the mongodb official website
Use the $ operator to return one or more, no way