在mongodb中选择id中存在数组字段的所有用户名
这是集合collu users中的字段在mongodb中选择id中存在数组字段的所有用户名,mongodb,select,Mongodb,Select,这是集合collu users中的字段 _id // ObjectID username // type string password // type string friend_id // array number ( ids of friends) user_id // Number (my id) 好的,我想从coll_用户中选择存在于friend_id中的所有用户名,我是说 db.coll_users.find() 我要做的是选择集合的friend\u id中存在user\u id的
_id // ObjectID
username // type string
password // type string
friend_id // array number ( ids of friends)
user_id // Number (my id)
好的,我想从coll_用户中选择存在于friend_id中的所有用户名,我是说
db.coll_users.find()
我要做的是选择集合的friend\u id中存在user\u id的所有用户
关于您应该使用
db.coll_users.find( { "_id" : ObjectId("51aaba74e747509139beed9b"),
"friend_id" : { $in : [ 2, 3, 55, 56 ] }, "password" : "something", "user_id" : 1,
"username" : "something" },
{"username" : 1 } )
您可以使用聚合框架:
db.coll_users.aggregate( [
{$unwind : "$friend_id"},
{$project: { selfAsFriend: {$eq:["$user_id","$friend_id"]}, user_id:1 } },
{$match : { selfAsFriend : true } }
] );
嗯,不,因为我想选择所有用户,用户id=friend,我想选择用户id=2、用户id=3、用户id=55和用户id=56的所有用户名称,你是指他/她自己的朋友吗?
db.coll_users.aggregate( [
{$unwind : "$friend_id"},
{$project: { selfAsFriend: {$eq:["$user_id","$friend_id"]}, user_id:1 } },
{$match : { selfAsFriend : true } }
] );