Home  >  Q&A  >  body text

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

原数据为:

{
    "_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
        }
    ]
}
伊谢尔伦伊谢尔伦2731 days ago683

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-27 09:04:20

    This is simple. Use the following statement to return only the current matching array:

    db.getCollection('web_mem_favorites').find({"_id":NumberLong(1181675746),"favorite_shards.sid":NumberLong(577)},{"favorite_shards.$":1}).pretty()

    reply
    0
  • PHP中文网

    PHP中文网2017-04-27 09:04:20

    javascriptdb.getCollection('web_mem_favorites').find(
        {'_id':NumberLong(1181675746)},favorite_shards.sid': {'$in':[NumberLong(577)]}}
        {"_id" : 1, "favorite_shards": "$slice[2,1]" }
    )
    

    When the favorite_shards array is returned, only the second array element is returned.
    But this requires knowing in advance which element sid:577 is.
    In the mongodb array query manual, there is no method that can return array units that meet custom conditions. You can try to use a program to filter the favorites_shards data on the returned result set.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-04-27 09:04:20

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

    I understand the meaning of the question, here is the modified code

    db.test.aggregate({"$unwind":"$favorite_shards"}, {"$match":{"favorite_shards.sid": {"$in": [NumberLong(578), NumberLong(577)]}}},  {"$group": {"_id": "$_id", "favorite_shards":{'$push': "$favorite_shards"}}})
    

    Result:
    { "_id" : NumberLong(1181675746), "favorite_shards" : [ { "sid" : NumberLong(578), "favorite_dt" : ISODate("2015-06-26T0406.405Z"), " is_attention" : true }, { "sid" : NumberLong(577), "favorite_dt" : ISODate("2015-06-26T0548.449Z"), "is_attention" : true } ] }

    reply
    0
  • 阿神

    阿神2017-04-27 09:04:20

    You can use the projection operator $elemMatch:

    javascriptdb.test.find({'favorite_shards.sid': 577}, {favorite_shards:{$elemMatch:{sid:577 }  } }).pretty()
    
    {
        "_id" : NumberLong(1181675746),
        "favorite_shards" : [
            {
                "sid" : NumberLong(577),
                "favorite_dt" : ISODate("2015-06-26T05:20:48.449Z"),
                "is_attention" : true
            }
        ]
    }
    

    The limitation of $elemMatch is that it can only return the first matching record in the array.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-27 09:04:20

    I would like to ask what software this is?

    reply
    0
  • 習慣沉默

    習慣沉默2017-04-27 09:04:20

    You can use $unwind to query, and multiple subdocuments that meet the query conditions will be returned.

    db.web_mem_favorites.aggregate(
        {"$project":{"favorite_shards":"$favorite_shards"}},
        {"$unwind":"$favorite_shards"},
        {"$match":{"favorite_shards.sid": NumberLong(577)}}
    )
    

    reply
    0
  • Cancelreply