SailsJS – Mongofind哪里或者哪个条件协会(一对一)

我有一个模型:

module.exports = { schema: true, attributes: { friend1: { model: "User", //Logged in User required: true }, friend2: { model: "User", //Profile Viewing User to whom I can send friend request required: true }, status: { type: "int", defaultsTo: "0" } } }; 

现在我试图检查是否有任何用户是我的朋友或以下书面filter,似乎不工作!

 Friend.find().where({ or: [ { friend1: user_id, friend2: fried_id }, { friend1: fried_id, friend2: user_id } ] }).exec(function findCB(err, Friend) { if(err) throw err; next(Friend); }); 

任何人都可以找出我在这里做错了吗?

有一个相关的closures问题, 如何使用search修改器“或”在Sails.js使用水线适配器的Mongo ,取决于您使用的版本。 作为一种解决方法,我只能build议您尝试使用native()来访问代表指定模型的原始Mongo集合实例,以允许您执行原始Mongo查询:

 var criteria = { "$or": [ { "friend1": user_id, "friend2": friend_id }, { "friend1": friend_id, "friend2": user_id } ] }; // Grab an instance of the mongo-driver Friend.native(function(err, collection) { if (err) return res.serverError(err); // Execute any query that works with the mongo js driver collection.find(criteria).toArray(function (err, friendObj) { if(err) throw err; console.log(friendObj); }); });