MongoDB查找数组查询

时间:2019-05-01 06:20:45

标签: node.js mongodb

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

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

//我的MongoDB集合

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController controller = TextEditingController();
  String aMostrar = "";
  @override
  Widget build(BuildContext context) {
    controller.text = "";
    controller.addListener(() {
      aMostrar = controller.text;
    });
    return Scaffold(
      appBar: AppBar(
        title: Text("TextField Example"),
      ),
      body: Container(
        margin: EdgeInsets.all(20.0),
        child: Center(
            child: Column(
          children: <Widget>[
            TextField(
              controller: controller,
            ),
            Text(aMostrar),
          ],
        )),
      ),
    );
  }
}

{

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

}

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

"_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

1 个答案:

答案 0 :(得分:0)

您只需要通过{posts:1}

尝试一下:

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