【问题标题】:Only one element returned in array数组中只返回一个元素
【发布时间】:2016-10-21 16:49:31
【问题描述】:

我正在尝试使用流星从我的 MongoDB 数据库中查找元素。 我设法过滤并检查了数组的结构,但结果是单个元素,而不是所有符合条件的元素。

查询:

var json = Tests1VerlIR.find({}, {fields: {entries: {$elemMatch: {'payload.id': {$eq: this.params._id}} } } }).fetch();
this.response.setHeader('Content-Type', 'application/json');
this.response.end(JSON.stringify(json));

数据结构:

{"entries":
   [{"method":"POST",
   "source":"ex",
   "path":"/ex",
   "time":1464615406900,
   "payload":        
        {"slot_frame_number":"4",
         "slot_HTUTemp":"2306",
         "data":"0400f008561655270209a314",
         "slot_BMEPres":"10069",
         "slot_HTUHumi":"5283",
         "slot_BMETemp":"2288",
         "time":"1464615404",
         "device":"79",
         "slot_BMEHumi":"5718",
         "signal":"7.22",
         "id":"2"},
   "_id":"574c41ee578d01af3664cbaf"},
   {"method":"POST",
   "source":"ex",
   "path":"/ex",
   "time":1464615406900,
   "payload":        
        {"slot_frame_number":"4",
         "slot_HTUTemp":"2306",
         "data":"0400f008561655270209a314",
         "slot_BMEPres":"10069",
         "slot_HTUHumi":"5283",
         "slot_BMETemp":"2288",
         "time":"1464615404",
         "device":"79",
         "slot_BMEHumi":"5718",
         "signal":"7.22",
         "id":"2"},
   "_id":"574c41ee578d01af3664cbaf"}, {...}]}

回应:

[
    {
        "_id":
        {
            "_str": "576155d7a605348159cd1f1a"
        },
        "entries":
        [
            {
                "method": "POST",
                "source": "ex",
                "path": "/ex",
                "time": 1464615406900,
                "payload":
                {
                     "slot_frame_number":"4",
                     "slot_HTUTemp":"2306",
                     "data":"0400f008561655270209a314",
                     "slot_BMEPres":"10069",
                     "slot_HTUHumi":"5283",
                     "slot_BMETemp":"2288",
                     "time":"1464615404",
                     "device":"79",
                     "slot_BMEHumi":"5718",
                     "signal":"7.22",
                     "id":"2"
                },
                "_id": "574c41ee578d01af3664cbaf"
            }
        ]
    }
]

【问题讨论】:

    标签: arrays mongodb meteor filter


    【解决方案1】:

    您不能以任何形式的基本 .find() 查询返回与您的条件匹配的数组的多个元素。要匹配多个元素,您需要改用 .aggregate() 方法。

    参考this link

    Tests1VerlIR.aggregate([
    { "$match": { "entries.payload.id": "2" } },
    
        // Unwind the array to denormalize
        { "$unwind": "$entries" },
    
        // Match specific array elements
        { "$match": { "entries.payload.id": "2" } },
    
        // Group back to array form
        { "$group": {
            "_id": "$_id",
            "entries": { "$push": "$entries" }
        }}
    ])
    

    【讨论】:

      【解决方案2】:

      解决方案:

      var json = Tests1VerlIR.aggregate({"$unwind": "$entries"}, {$match: {'entries.payload.id': this.params._id} });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        • 1970-01-01
        相关资源
        最近更新 更多