如何在mongoDb中进行过滤?

时间:2018-09-03 09:44:22

标签: mongodb mongodb-query

我有一个像下面这样存储的收藏

 "role" : [ 
                {
                    "role_id" : "599adabbef17f85afbcee00d",
                    "earn" : {
                        "5943c3f7c74b036a883efaf0" : "E2",
                        "5943c46ec74b036a883efaf1" : "E0",
                        "594a0b08c74b030a2ae33cce" : "E2",
                        "5943c3f7c74b036a883efaf0" : "E5",
                        "5943c46ec74b036a883efaf1" : "E0",
                        "594a0b08c74b030a2ae33cce" : "E4"
                    }
                 }
           ]

我只需要获取不带有“ E0”的挣值。

 "5943c3f7c74b036a883efaf0" : "E2",
 "594a0b08c74b030a2ae33cce" : "E2",
 "5943c3f7c74b036a883efaf0" : "E5",
 "594a0b08c74b030a2ae33cce" : "E4"

2 个答案:

答案 0 :(得分:1)

您可以按以下方式使用聚合查询

db.roles.aggregate([
  {$unwind: "$role" },
  {$project : { earn : { $objectToArray: "$role.earn" }}},
  {$unwind : "$earn" },
  {$match: { "earn.v" : { $ne : "E0" }}},
]);

在以下集合上执行

> db.roles.find().pretty()
{
        "_id" : ObjectId("5b8d4db5217b959d85f79899"),
        "role" : [
                {
                        "role_id" : "599adabbef17f85afbcee00d",
                        "earn" : {
                                "5943c3f7c74b036a883efaf0" : "E5",
                                "5943c46ec74b036a883efaf1" : "E0",
                                "594a0b08c74b030a2ae33cce" : "E4"
                        }
                }
        ]
}

您将获得以下输出

> db.roles.aggregate([
...   {$unwind: "$role" },
...   {$project : { earn : { $objectToArray: "$role.earn" }}},
...   {$unwind : "$earn" },
...   {$match: { "earn.v" : { $ne : "E0" }}},
... ]);
{ "_id" : ObjectId("5b8d4db5217b959d85f79899"), "earn" : { "k" : "5943c3f7c74b036a883efaf0", "v" : "E5" } }
{ "_id" : ObjectId("5b8d4db5217b959d85f79899"), "earn" : { "k" : "594a0b08c74b030a2ae33cce", "v" : "E4" } }

答案 1 :(得分:0)

考虑此架构:

{ 
    "_id" : ObjectId("5b8d29d239bd595000edf3ee"), 
    "role" : [
        {
            "role_id" : "599adabbef17f85afbcee00d", 
            "earn" : {
                "5943c3f7c74b036a883efaf5" : "E2", 
                "5943c46ec74b036a883efaf1" : "E0", 
                "594a0b08c74b030a2ae33cce" : "E2", 
                "5943c3f7c74b036a883efaf0" : "E5", 
                "5943c46ec74b036a883efab1" : "E0", 
                "594a0b08c74b030a2ae34cce" : "E4"
            }
        }, 
        {
            "role_id" : "599adabbef17f85afbcee02d", 
            "earn" : {
                "6943c3f7c74b036a883efaf5" : "E2", 
                "6943c46ec74b036a883efaf1" : "E0", 
                "694a0b08c74b030a2ae33cce" : "E2", 
                "6943c3f7c74b036a883efaf0" : "E5", 
                "6943c46ec74b036a883efab1" : "E0", 
                "694a0b08c74b030a2ae34cce" : "E4"
            }
        }
    ]
}

尝试以下汇总

   { 
    "_id" : ObjectId("5b8d29d239bd595000edf3ee"), 
    "role" : [
        {
            "role_id" : "599adabbef17f85afbcee00d", 
            "earn" : {
                "5943c3f7c74b036a883efaf5" : "E2", 
                "5943c46ec74b036a883efaf1" : "E0", 
                "594a0b08c74b030a2ae33cce" : "E2", 
                "5943c3f7c74b036a883efaf0" : "E5", 
                "5943c46ec74b036a883efab1" : "E0", 
                "594a0b08c74b030a2ae34cce" : "E4"
            }
        }, 
        {
            "role_id" : "599adabbef17f85afbcee02d", 
            "earn" : {
                "6943c3f7c74b036a883efaf5" : "E2", 
                "6943c46ec74b036a883efaf1" : "E0", 
                "694a0b08c74b030a2ae33cce" : "E2", 
                "6943c3f7c74b036a883efaf0" : "E5", 
                "6943c46ec74b036a883efab1" : "E0", 
                "694a0b08c74b030a2ae34cce" : "E4"
            }
        }
    ]
}

将返回:

{ 
    "5943c3f7c74b036a883efaf5" : "E2", 
    "594a0b08c74b030a2ae33cce" : "E2", 
    "5943c3f7c74b036a883efaf0" : "E5", 
    "594a0b08c74b030a2ae34cce" : "E4", 
    "6943c3f7c74b036a883efaf5" : "E2", 
    "694a0b08c74b030a2ae33cce" : "E2", 
    "6943c3f7c74b036a883efaf0" : "E5", 
    "694a0b08c74b030a2ae34cce" : "E4"
}

希望有帮助...