0

I am trying to get building by its id:

I have a collection called buildings:

{
    "_id" : ObjectId("5b3b1cc79c23061e4d4634e4"),
    "buildings" : [ 
        {
            "id" : 0,
            "name" : "Farm",
            "description" : "Best farm of all times",
            "img" : "http://i.hizliresim.com/yq5g57.png",
            "wood" : "50",
            "stone" : "10"
        }, 
        {
            "id" : 1,
            "name" : "Storage",
            "description" : "Store your resources.",
            "img" : "http://i.hizliresim.com/yq5g47.png",
            "wood" : "100",
            "stone" : "200"
        }
    ]
}

For example with id 0,i would like to get data of Farm.

I tried this: db.getCollection('buildings').find({"buildings.id":0})

not working

sample output :

{
                "id" : 0,
                "name" : "Farm",
                "description" : "Best farm of all times",
                "img" : "http://i.hizliresim.com/yq5g57.png",
                "wood" : "50",
                "stone" : "10"
            }

Tried:

var data = Buildings.find({},{buildings:{$elemMatch:{id:0}}}).fetch();
console.log(JSON.stringify(data));

result:(all data)

[{"_id":{"_str":"5b3b1cc79c23061e4d4634e4"},"buildings":[{"id":0,"name":"Farm","description":"Best farm of all times","img":"http://i.hizliresim.com/yq5g57.png","wood":"50","stone":"10"},{"id":1,"name":"Storage","description":"Store your resources.","img":"http://i.hizliresim.com/yq5g47.png","wood":"100","stone":"200"}]}]
9
  • you have put extra curly braces... Remove them... Try this db.collection.find({ "buildings.id": 0 }) Commented Jul 3, 2018 at 11:28
  • i did it gave me all data json.stringfy result : [{"_id":{"_str":"5b3b1cc79c23061e4d4634e4"},"buildings":[{"id":0,"name":"Farm","description":"Best farm of all times","img":"i.hizliresim.com/…"},{"id":1,"name":"Storage","description":"Store your resources.","img":"i.hizliresim.com/…"}]}] Commented Jul 3, 2018 at 11:31
  • so what you want? Commented Jul 3, 2018 at 11:37
  • just "id":0 s data in this case { "id" : 0, "name" : "Farm", "description" : "Best farm of all times", "img" : "i.hizliresim.com/yq5g57.png", "wood" : "50", "stone" : "10" }, Commented Jul 3, 2018 at 11:41
  • Use elemmatch(projection). more here i.e db.getCollection('buildings').find({},{buildings:{$elemMatch:{id:0}}}) Commented Jul 3, 2018 at 12:05

3 Answers 3

1

You can use $filter aggregation to exclude the unwanted elements from the array

db.collection.aggregate([
  { "$match": { "buildings.id": 0 }},
  { "$project": {
    "shapes": {
      "$arrayElemAt": [
        { "$filter": {
          "input": "$buildings",
          "as": "building",
          "cond": {
            "$eq": [
              "$$building.id",
              0
            ]
          }
        }},
        0
      ]
    },
    "_id": 0
  }}
])
Sign up to request clarification or add additional context in comments.

1 Comment

then please post the sample output you want from the collection you have posted
1

Try for this

db.getCollection("collectionName").find({buildings: {"$elemMatch": {"id" : "0"}}})

Here the find method will look(cursor) for the data with buildings and id=0

Comments

1
db.collection.find({
    buildings: {
        $elemMatch: {
            id: 0
        }
    }
}, {
    'buildings.$': 1
})

1 Comment

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

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.