简体   繁体   中英

How to query element of array of document in mongodb

i just spent the whole evening trying to figure this out, I have users in db like this:

[
    {
        "_id": "5fcff6ed224670346c8b60de",
        "name": "john",
        "password": "147",
        "data": [
            {
                "_id": "5fd000356d5df538d0338db4",
                "comment": "comment1",
                "date": "2020-12-08T22:37:41.740Z"
            },
            {
                "_id": "5fd0003e6d5df538d0338db6",
                "comment": "comment2",
                "date": "2020-12-08T22:37:50.271Z"
            },
            {
                "_id": "5fd000476d5df538d0338dba",
                "comment": "comment3",
                "date": "2020-12-08T22:37:59.128Z"
            }
        ],
        "__v": 6
    },
    {
        "_id": "5fcff6f5224670346c8b60df",
        "name": "Samantha",
        "password": "123",
        "data": [],
        "__v": 0
    },
]

when user "john" is logged in, and he wants to retrieve a comment No. 1, how can I do it?

I figured out how to delete it, but can't know how to retrieve it

router.delete("/:id", auth, async (req, res) => {
  let user = await User.findByIdAndUpdate(
    req.user._id,
    {
      $pull: {
        data: { _id: req.params.id },
      },
    },
    { new: true }
  );
  res.send(user.data);
});

I even gave up and tried to just retrieve the whole user and then filter the data but it didn't work

router.get("/:id", auth, async (req, res) => {
  let user = await User.findById(req.user._id);
  let arr = user.data
  arr = arr.filter(e => e._id === req.params.id)
  res.send(arr)
});

You can use the query and projection like so:

db.getCollection('users').find({
  "data": {
    $elemMatch: {
      comment: "comment1"
    }
  },
  "name": "john"
},
{
  "data.$": 1
})

With $elemMatch you can specify multiple criteria on your embedded document.

Then, you project the result as returning only the first element with the conditions of
{comment: "comment1"} and {name: "john"}

You will get the data array with only one index which is what you asked for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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