MongoDB select where in array of _id?

前端 未结 5 1103
眼角桃花
眼角桃花 2020-11-29 23:59

is possible in mongo db to select collection\'s documents like in SQL :

SELECT * FROM collection WHERE _id IN (1,2,3,4);

or if i have a

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 00:38

    Because mongodb uses bson and for bson is important attribute types. and because _id is ObjectId you must use like this:

    db.collection.find( { _id : { $in : [ObjectId('1'),ObjectId('2')] } } );
    

    and in mongodb compass use like this:

    { "_id" : { $in : [ObjectId('1'),ObjectId('2')] } }
    

    Note: objectId in string has 24 length.

提交回复
热议问题