2
votes

I am using sailsjs mongodb and node.js i am getting error following error in mongodb query please help!
i want to get result of those messages which match Exactly with $all: [ senderID , sendToID ]

this is my document "message" in mongodb.

{
    "users": [
        "52ed09e1d015533c124015d5",
        "52ed4bc75ece1fb013fed7f5" 
    ],
    "user_msgs": [],
    "createdAt": ISODate("2014-02-04T11:59:53.220Z"),
    "updatedAt": ISODate("2014-02-04T11:59:53.220Z"),
    "_id": ObjectID("52f0d639b922c9142763c336")
}

now i want to query

    Message.find({ users : {  $all:  [ msg.sender ,   msg.sendTo ]  } })

                    .done(function (err, detail) {
                if (err) {              
                    console.log(err)
                  } else {

                    console.log( detail)}

          });

which returns error

{[MongoError:$all requires array] name:'MongoError'} 

i am following documentation http://docs.mongodb.org/manual/reference/operator/query/all/ but still have no idea what is causing problem

1
Are msg.sender and msg.sendTo valid?robertklep
msg.sender and msg.sendTo is a valid value like "52ed09e1d015533c124015d5"Abdul Aleem
have you tested your query in the mongo shell to isolate where the problem is coming from? A simple test there also worked for me...chk
@chk i am using sailsjs which have a implements waterline and also sails-mongo...Abdul Aleem
@AbdulAleem can you test queries directly with the mongo shell nevertheless? Just to check if the query itself works, in that case it would probably be a DB driver issue.chk

1 Answers

1
votes

In Waterline there is not a way currently to query embedded records. You can drop down to the native mongo driver in sails-mongo though and run queries. The following should give you what you need.

User.native(function(err, collection) {
    collection.find({ users: { 
        $all: [ "52ed09e1d015533c124015d5", "52ed4bc75ece1fb013fed7f5" ] 
    }}).toArray(function(err, docs) {
       // Do something here
    });
});