0

これはコレクション coll_users のフィールドです

_id // ObjectID
username // type string
password // type string
friend_id // array number ( ids of friends)
user_id // Number (my id)

わかりました、friend_id に存在する coll_users からすべてのユーザー名を選択したいということですか?

デシベル.coll_users.find()

{ "_id" : ObjectId("51aaba74e747509139beed9b"), "friend_id" : [ 2, 3, 55, 56 ], "password" : "something", "user_id" : 1, "username" : "something" }

私がやりたいのは、コレクションのfriend_idにuser_idが存在するすべてのユーザーを選択することです

よろしく

4

2 に答える 2

1

集計フレームワークを使用できます。

db.coll_users.aggregate( [ 
   {$unwind : "$friend_id"},
   {$project: { selfAsFriend: {$eq:["$user_id","$friend_id"]}, user_id:1 } },
   {$match  : { selfAsFriend : true } }
] );
于 2013-06-02T19:00:31.317 に答える