mongodb 内嵌数组查询问题: 如何限定返回与条件匹配的数组-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

mongodb 内嵌数组查询问题: 如何限定返回与条件匹配的数组

蛮大人123 2016-06-14 16:12:44 1713

screenshot
原数据为:

{
    "_id" : NumberLong(1181675746),
    "shard_qty" : 4,
    "goods_qty" : 0,
    "shop_qty" : 0,
    "favorite_qty" : 4,
    "favorite_shards" : [ 
      {
            "sid" : NumberLong(580),
            "favorite_dt" : ISODate("2015-06-26T12:13:06.405+08:00"),
            "is_attention" : true
        }, 
      {
            "sid" : NumberLong(579),
            "favorite_dt" : ISODate("2015-06-26T12:13:06.405+08:00"),
            "is_attention" : true
        }, 
        {
            "sid" : NumberLong(578),
            "favorite_dt" : ISODate("2015-06-26T12:13:06.405+08:00"),
            "is_attention" : true
        }, 
        {
            "sid" : NumberLong(577),
            "favorite_dt" : ISODate("2015-06-26T13:20:48.449+08:00"),
            "is_attention" : true
        }
    ]
}

查询条件为

db.getCollection('web_mem_favorites').findOne(
    {
    '_id':NumberLong(1181675746),
    'favorite_shards.sid': {
        '$in':[NumberLong(577),NumberLong(578)]
        }
    }
    ,{"favorite_shards":1}
)

想返回的数据:

{
    "_id" : NumberLong(1181675746),
    "favorite_shards" : [ 
      {
            "sid" : NumberLong(578),
            "favorite_dt" : ISODate("2015-06-26T12:13:06.405+08:00"),
            "is_attention" : true
        }, 
        {
            "sid" : NumberLong(577),
            "favorite_dt" : ISODate("2015-06-26T13:20:48.449+08:00"),
            "is_attention" : true
        }
    ]
}
NoSQL MongoDB
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 19:38:00
    {
    
    "_id" : NumberLong(1181675746),
    "shard_qty" : 4,
    "goods_qty" : 0,
    "shop_qty" : 0,
    "favorite_qty" : 4,
    "favorite_shards" : [
        {
            "sid" : NumberLong(580),
            "favorite_dt" : ISODate("2015-06-26T04:13:06.405Z"),
            "is_attention" : true
        },
        {
            "sid" : NumberLong(579),
            "favorite_dt" : ISODate("2015-06-26T04:13:06.405Z"),
            "is_attention" : true
        },
        {
            "sid" : NumberLong(578),
            "favorite_dt" : ISODate("2015-06-26T04:13:06.405Z"),
            "is_attention" : true
        },
        {
            "sid" : NumberLong(577),
            "favorite_dt" : ISODate("2015-06-26T05:20:48.449Z"),
            "is_attention" : true
        }
    ]
    }
    
    db.test3.aggregate([
    
    ... {"$match":{"_id":NumberLong(1181675746)}},
    ... {"$project":{"_id":1,"favorite_shards":1}},
    ... {"$unwind": "$favorite_shards"},
    ... {$match: {"favorite_shards.sid": {'$in':[NumberLong(577),NumberLong(578)]}}},
    ... {"$group": {"_id": "$_id", "favorite_shards": {"$push": "$favorite_shards"}}}
    ... ]).pretty()
    {
    
    "_id" : NumberLong(1181675746),
    "favorite_shards" : [
        {
            "sid" : NumberLong(578),
            "favorite_dt" : ISODate("2015-06-26T04:13:06.405Z"),
            "is_attention" : true
        },
        {
            "sid" : NumberLong(577),
            "favorite_dt" : ISODate("2015-06-26T05:20:48.449Z"),
            "is_attention" : true
        }
    ]
    }

    实现的逻辑就是将原来的数组拆开,然后重组就好了。

    0 0
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题
推荐课程