2
votes
sails.js - sailsjs array query Exact match - Stack Overflow
Asked
Viewed 1k times
2

i want to query mongodb in sailsjs. this is structure of my db

{
    "users": [
        "52ed09e1d015533c124015d5",
        "52ed4bc75ece1fb013fed7f5"
    ],
    "user_msgs": [
        {
            "sender": "52ed09e1d015533c124015d5",
            "sendTo": "52ed4bc75ece1fb013fed7f5",
            "msg": "ss"
        }
    ],
    "createdAt": ISODate("2014-02-06T16:12:17.751Z"),
    "updatedAt": ISODate("2014-02-06T16:12:17.751Z"),
    "_id": ObjectID("52f3b461f46da23c111582f6")
}

I want to search those "users" who who match array

  [
        "52ed09e1d015533c124015d5",
        "52ed4bc75ece1fb013fed7f5"
    ]
Message.find({user: ["52ed09e1d015533c124015d5","52ed4bc75ece1fb013fed7f5"]})

this query returns all objects which contains 1 OR 2 ..but i need only those which exacly match 1 AND 2, i have also tried $all, and etc.. but did not worked please tell me how to write query with sailsjs supported syntex to get those user
    1

    You'll need to use the native Mongo adapter for this:

    Message.native(function(err, collection) {
    
       collection.find({users:{'$all':["52ed09e1d015533c124015d5","52ed4bc75ece1fb013fed7f5"]}}).toArray(function(err, results) {
    
         // Do something with results
    
       });
    
    });
    
      -1
      Message.find()
      .where({users: "52ed09e1d015533c124015d5", "52ed4bc75ece1fb013fed7f5"})
      .exec(function(){
        // do something
      });
      

      While the above code may work to pull in just those users. I think a better solution would be to define your user ID's in your message model.

      I would add the following attributes to your messages model:

      senderID: {
         type: 'string'
      },
      receiverID: {
         type: 'string'
      }
      

      Now you can make that query more efficient by using the following query:

      Message.find()
      .where({senderID: "52ed09e1d015533c124015d5"})
      .where({receiverID: "52ed4bc75ece1fb013fed7f5"})
      .exec(function(){
        // do something
      });
      

      This is the route I would take.

      2
      • Thanks for your answer .but it did not worked for me. the above schema i made for mongodb to allow 1-1 and multi-user chat..like group conversation.in "users" array i want to stroe those user ID's that are in this chat.and while retriving data i want the EXACT array of user.. where also returns all objects who have one of them Feb 7 2014 at 14:19
      • This is incorrect syntax: .where({users: "52ed09e1d015533c124015d5", "52ed4bc75ece1fb013fed7f5"}) Jan 3 2017 at 2:52

      Your Answer

      By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

      Not the answer you're looking for? Browse other questions tagged or ask your own question.

       
      2

      2 Answers

      1
      votes

      You'll need to use the native Mongo adapter for this:

      Message.native(function(err, collection) {
      
         collection.find({users:{'$all':["52ed09e1d015533c124015d5","52ed4bc75ece1fb013fed7f5"]}}).toArray(function(err, results) {
      
           // Do something with results
      
         });
      
      });
      
      -1
      votes
      Message.find()
      .where({users: "52ed09e1d015533c124015d5", "52ed4bc75ece1fb013fed7f5"})
      .exec(function(){
        // do something
      });
      

      While the above code may work to pull in just those users. I think a better solution would be to define your user ID's in your message model.

      I would add the following attributes to your messages model:

      senderID: {
         type: 'string'
      },
      receiverID: {
         type: 'string'
      }
      

      Now you can make that query more efficient by using the following query:

      Message.find()
      .where({senderID: "52ed09e1d015533c124015d5"})
      .where({receiverID: "52ed4bc75ece1fb013fed7f5"})
      .exec(function(){
        // do something
      });
      

      This is the route I would take.