0

I'm new to mongodb and I've been trying to query this doc for awhile now.

Im trying to query all the rooms that have a room name of 100.

json

{
"count": 3,
"reviews": [
    {
        "_id": "5f9d42a0a8e71e004643f584",
        "user": {
            "_id": "5f7308cde0a4a7a66bc3d184",
            "name": "Guest"
        },
        "room": {
            "_id": "5f98f9321fd5bb0045b3d886",
            "name": "100",
        },
        "rating": 4,
    },
    {
        "_id": "5f9d431ea8e71e004643f585",
        "user": {
            "_id": "5f7308cde0a4a7a66bc3d184",
            "name": "Guest",
        },
        "room": {
            "_id": "5f98f9321fd5bb0045b3d886",
            "name": "100",
        },
        "rating": 5,
    },
    {
        "_id": "5f9e74fea6c06a0046d3cae2",
        "user": {
            "_id": "5f7308cde0a4a7a66bc3d184",
            "name": "Guest",
        },
        "room": {
            "_id": "5f98fa8b1fd5bb0045b3d88a",
            "name": "300",
        },
        "rating": 5,   
    }
]}

what I've tried

1. find({},{"reviews.room.name": {$eq: "100"}}) // getting a projection error
2. find({"reviews.room.name": "100"}) // getting null

Any help to the right direction would be greatly appreciated.

4
  • Try find({"reviews": { $elemMatch:{"room.name": "100"} } }) Commented Nov 1, 2020 at 10:53
  • I'm getting an empty array as the response Commented Nov 1, 2020 at 11:00
  • Are you sure? It works as it should: mongoplayground.net/p/iiBj8hITbPB Commented Nov 1, 2020 at 16:24
  • Thanks for your effort.But I've already found a solution.I think my problem was confusing the mongo-db structure. The reviews was my own created object my bad. Commented Nov 1, 2020 at 22:07

2 Answers 2

1

Check out the docs about how to use find: https://docs.mongodb.com/manual/reference/method/db.collection.find/

and try this

db.reviews.find({
  "room.name": "100"
})

Here is the playground with your example: https://mongoplayground.net/p/dPfH5fSOePq

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this works. I think I was confusing the mongo-db structure.
I'm glad if it helped :)
0

did u try this ? const response = await Room.find({name: "100"})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.