Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查询属性数组mongodb_Mongodb_Meteor_Mongodb Query - Fatal编程技术网

查询属性数组mongodb

查询属性数组mongodb,mongodb,meteor,mongodb-query,Mongodb,Meteor,Mongodb Query,我正在尝试通过其id获取建筑物: 我有一个名为“建筑物”的收藏: { "_id" : ObjectId("5b3b1cc79c23061e4d4634e4"), "buildings" : [ { "id" : 0, "name" : "Farm", "description" : "Best farm of all times", "img" : "http://i

我正在尝试通过其id获取建筑物:

我有一个名为“建筑物”的收藏:

{
    "_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"
        }
    ]
}
例如,id为0的情况下,我希望获取农场的数据

我试过这个: db.getCollection('buildings').find({“buildings.id”:0})

不起作用

样本输出:

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

var data = Buildings.find({},{buildings:{$elemMatch:{id:0}}}).fetch();
console.log(JSON.stringify(data));
结果:(所有数据)

可以使用聚合从数组中排除不需要的元素

db.collection.aggregate([
  { "$match": { "buildings.id": 0 }},
  { "$project": {
    "shapes": {
      "$arrayElemAt": [
        { "$filter": {
          "input": "$buildings",
          "as": "building",
          "cond": {
            "$eq": [
              "$$building.id",
              0
            ]
          }
        }},
        0
      ]
    },
    "_id": 0
  }}
])
可以使用聚合从数组中排除不需要的元素

db.collection.aggregate([
  { "$match": { "buildings.id": 0 }},
  { "$project": {
    "shapes": {
      "$arrayElemAt": [
        { "$filter": {
          "input": "$buildings",
          "as": "building",
          "cond": {
            "$eq": [
              "$$building.id",
              0
            ]
          }
        }},
        0
      ]
    },
    "_id": 0
  }}
])
试试这个

db.getCollection("collectionName").find({buildings: {"$elemMatch": {"id" : "0"}}})
在这里,find方法将查找(光标)带有建筑物的数据,id=0

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

在这里,find方法将查找(光标)带有建筑物的数据,id=0

您已经放置了额外的大括号。。。移除它们。。。试试这个
db.collection.find({“buildings.id”:0})
我做了它给了我所有的数据json.stringfy结果:[{“{”id:{”str:“5b3b1cc79c23061e4d4634e4”},“buildings:[{”id:“0”,name:“Farm”,“description:“有史以来最好的农场”,“img:”},{”id:“1,“name:“Storage”,“description:“Store your resources.”,“,“img:”}]那么你想要什么呢?只要“id”:0s数据在本例中{“id”:0,“name”:“Farm”,“description”:“有史以来最好的农场”,“img”:“wood”:“50”,“stone”:“10”},请使用elemmatch(投影)。更多信息,例如
db.getCollection('buildings')。查找({},{buildings:{$elemMatch:{id:0}}}})
您添加了额外的花括号。。。移除它们。。。试试这个
db.collection.find({“buildings.id”:0})
我做了它给了我所有的数据json.stringfy结果:[{“{”id:{”str:“5b3b1cc79c23061e4d4634e4”},“buildings:[{”id:“0”,name:“Farm”,“description:“有史以来最好的农场”,“img:”},{”id:“1,“name:“Storage”,“description:“Store your resources.”,“,“img:”}]那么你想要什么呢?只要“id”:0s数据在本例中{“id”:0,“name”:“Farm”,“description”:“有史以来最好的农场”,“img”:“wood”:“50”,“stone”:“10”},请使用elemmatch(投影)。更多信息,例如
db.getCollection('buildings').find({},{buildings:{$elemMatch:{id:0}}}}})
然后请发布您所发布的集合中的示例输出,然后请发布您所发布的集合中的示例输出,而此代码片段可能是解决方案,确实有助于提高发布的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。虽然此代码片段可能是解决方案,但确实有助于提高您文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
db.collection.find({
    buildings: {
        $elemMatch: {
            id: 0
        }
    }
}, {
    'buildings.$': 1
})