如何检查mongodb结构

时间:2017-08-30 17:26:11

标签: mongodb

我有一个mongodb集合,其中对象的结构如下:

{
  "id": "1234",
  "history": [
    {
      "userid": 100,
      "myobjects": [{id, id1, id4}]
    },
    {
      "userid": 200,
      "myobjects": [{id2, id3, id5}]
    },
}

目标:如果我的用户ID为100,则返回其历史记录中不包含我的用户ID的对象。我猜它是某种“我的用户ID不在历史字段的键中”,但我不知道如何写出来。这是我的基本想法:

Collection.findOne(
  {
    in_progress : null, 
    history : {"$nin": myuserid } ???
  }
);

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

使用MongoDB aggregation

db.test.aggregate([
    {
        "$unwind" : "$history"
    },
    {
        "$match" : {
            "history.userid" : {
                "$ne" : 100
            }
        }
    },
    {
    "$group" : {
        "_id" : "$id",
        "history" : {
            "$push" : "$history"
        }
    }
}
]);
相关问题