Juyal Ahmed
Juyal Ahmed

Reputation: 118

SailsJS - Mongo find with where or condition Association (one to one)

I have a model:

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

Now I am trying to check if any user is my friend or not with below written filter, seems it is not working!

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

Can anyone figure me out what I am doing wrong here?

Upvotes: 3

Views: 766

Answers (1)

chridam
chridam

Reputation: 103355

There is somehow a related closed issue for that How to use Search Modifier “OR” in Sails.js using Waterline Adapter for Mongo, depending on the version you're using. As a workaround, I can only suggest you try using the native() which accesses a raw Mongo collection instance representing the specified model, allowing you to perform raw Mongo queries:

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

Upvotes: 2

Related Questions