node.js - SailsJS - Mongo 查找与位置或条件关联(一对一)

标签 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 ,似乎它不起作用!

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

谁能弄清楚我在这里做错了什么?

最佳答案

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

关于node.js - SailsJS - Mongo 查找与位置或条件关联(一对一),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33915933/

相关文章:

javascript - Async Await 是否违反了 sails 1.0 的 MVC?

MongoDB 查找并返回所有的最大值

javascript - 如何让 learnyounode #9 杂耍异步工作

node.js - 如何在ejs中将变量设置为Date类型?

ruby - mongoid 中的嵌入式文档与哈希数据类型

node.js - Mongoose:findOneAndUpdate 不返回更新的文档

javascript - 关于 Web 应用程序端口的混淆

javascript - 无法进入异步(同步)函数/空结果的回调

javascript - sails js 上的自定义和多语言消息错误如何解决

node.js - 尝试渲染 iframe : ancestor violates the following Content Security Policy directive: "frame-ancestors ' none'"