繁体   English   中英

MongoDB查找数组查询

[英]Mongodb find query for array

我正在尝试-在mongodb中查找数组查询,

  • db.users.find({posts:{$ all:[_ id:ObjectId(“ 5cc83a158ceb346aa58b1161”)]}}))
  • db.users.find({“ posts.post”:{$ all:[]}})

//我的MongoDB集合

> db.users.find().pretty()

{

"_id" : ObjectId("5cc83a158ceb346aa58b1161"),
"friends" : [ ],
"name" : "krishna soni",
"email" : "krishnasoni@gmail.com",
"password" : "krishna",
"posts" : [
    {
        "_id" : ObjectId("5cc8478118195b71e2ccd517"),
        "post" : "hey users",
        "createdTime" : ISODate("2019-04-30T13:02:57.621Z")
    },
    {
        "_id" : ObjectId("5cc853ec0a5fde0e431898f3"),
        "post" : "hey friends it's my final post",
        "createdTime" : ISODate("2019-04-30T13:55:56.119Z")
    }
],
"__v" : 0

}

//预期结果如下//显示给定objectId的posts数组

"posts" : [
    {
        "_id" : ObjectId("5cc8478118195b71e2ccd517"),
        "post" : "hey users",
        "createdTime" : ISODate("2019-04-30T13:02:57.621Z")
    },
    {
        "_id" : ObjectId("5cc853ec0a5fde0e431898f3"),
        "post" : "hey friends it's my final post",
        "createdTime" : ISODate("2019-04-30T13:55:56.119Z")
    }
],

您只需要通过{posts:1}

尝试这个:

db.users.find({"_id" : ObjectId("5cc83a158ceb346aa58b1161")},{posts:1});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM