[英]How to get some documents of array in MongoDB
你可以像這樣使用切片
db.users.find({},{"questions":{$slice:1}})
希望對你有幫助
使用$elemMatch
投影可獲得所需的結果。 以下find()
操作查詢所有文檔,其中$elemMatch
投影僅返回questions
數組的第一個匹配元素,其中question
字段的值為"dawdaw"
並且answered
值為0
:
db.users.find({},
{
"username": 1, "password": 1,
"questions": {
"$elemMatch": {
"question" : "dawdaw",
"answered" : 0
}
}
}
);
從給出的樣本中,該操作返回以下文檔:
/* 0 */
{
"_id" : ObjectId("561a84ffaa233b38d803509a"),
"username" : "asd@mail.ru",
"password" : "asd",
"questions" : [
{
"question" : "dawdaw",
"answered" : 0
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.