SailsJS - Mongo找到哪里或条件关联(一对一)

时间:2015-11-25 11:57:30

标签: node.js mongodb sails.js waterline

我有一个模特:

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"
    }
  }
};

现在我正在尝试使用下面写的过滤器检查是否有任何用户是我的朋友,似乎它不起作用!

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);
});

有谁能告诉我这里我做错了什么?

1 个答案:

答案 0 :(得分:1)

根据您使用的版本,How to use Search Modifier “OR” in Sails.js using Waterline Adapter for Mongo有某个相关的已关闭问题。作为解决方法,我只建议您尝试使用 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);
    });
});