【问题标题】:Getting only last field in mongoose $in Operator仅获取 mongoose $in 运算符中的最后一个字段
【发布时间】:2019-05-01 09:53:59
【问题描述】:

我正在尝试查找从开始到结束之间的所有人员记录,并在[1,2] 中有地点 ID 这是我的查询:

   Person.find({
        time: {
            $gte: start,
            $lt: end
        },
        "place.place_id": { $in: [1,2] }
    }, {
        "time":1,
        "place.$": 1
    },
    function (err, docs) {
        if (err) {
            res.status(500).send(err);
        } else {
            res.status(200).send(docs);
        }
    }
);

但我只得到一个位置。这是我的输出

[
{
    "_id": "5bffc1e9bd35e42020c05cf1",
    "time": "2018-11-29T10:38:01.401Z",
    "places": [
        {
            "_id": "5bffc1e9bd35e42020c05de3",
            "place_id": 1,
            "place_name": "test1"
        }
    ]
},
{
    "_id": "5bffc256bd35e42020c05de4",
    "time": "2018-11-29T10:40:01.324Z",
    "places": [
        {
            "_id": "5bffc256bd35e42020c05ed6",
            "place_id": 1,
            "place_name": "test1",
        }
    ]
}
]

我只得到 id 为 1 的地方,我想要的输出是

[
{
    "_id": "5bffc1e9bd35e42020c05cf1",
    "time": "2018-11-29T10:38:01.401Z",
    "places": [
        {
            "_id": "5bffc1e9bd35e42020c05de3",
            "place_id": 1,
            "place_name": "test1"
        },
        {
            "_id": "5bffc1e9bd35e42020c05de3",
            "place_id": 2,
            "place_name": "test2"
        }
    ]
},
{
    "_id": "5bffc256bd35e42020c05de4",
    "time": "2018-11-29T10:40:01.324Z",
    "places": [
        {
            "_id": "5bffc256bd35e42020c05ed6",
            "place_id": 1,
            "place_name": "test1",
        },
        {
            "_id": "5bffc256bd35e42020c05ed6",
            "place_id": 2,
            "place_name": "test2",
        }
    ]
}
]

更新 试过这个,但这给了我空对象

    Persons.find({
        time: {
            $gte: start,
            $lt: end
        },
        places: {
            $filter: {
               input: "$places",
               as: "place",
               cond: { $in: ["$$place.place_id",[ 1,2 ]] }
            }
         }  
    },
    function (err, docs) {
        if (err) {
            res.status(500).send(err);
        } else {
            res.status(200).send(docs);
        }
    }
);

};

我做错了什么??我还尝试了 $elemMatch、$all 运算符,但输出相同。

我怎样才能得到所有这些地方?

谢谢。

更新2

我每 2 分钟输入一次数据,这是开始值和结束值

var end = new Date();
var start = new Date(end);
start.setMinutes(end.getMinutes() - 10);

【问题讨论】:

  • 您需要使用$filter 聚合来获取place_idstackoverflow.com/questions/3985214/…的可能欺骗
  • @Unknown start & end 的值是多少?您能否在上面更新它们?
  • @Schüler 我已经更新了我的问题

标签: javascript node.js mongodb mongoose aggregation-framework


【解决方案1】:

您需要为此使用聚合。像

db.collection.aggregate([
  { "$match": {
    "time": { "$gte": start, "$lt": end },
    "place.place_id": { "$in": [1, 2] }
  }},
  { "$project": {
    "time": 1,
    "places": {
      "$filter": {
        "input": "$places",
        "as": "place",
        "cond": { "$in": ["$$place.place_id", [1, 2]] }
      }
    }
  }}
])

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2012-05-21
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多