2

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

6
  • Are msg.sender and msg.sendTo valid? Commented Feb 4, 2014 at 14:55
  • msg.sender and msg.sendTo is a valid value like "52ed09e1d015533c124015d5" Commented Feb 4, 2014 at 15:02
  • 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... Commented Feb 5, 2014 at 13:53
  • @chk i am using sailsjs which have a implements waterline and also sails-mongo... Commented Feb 6, 2014 at 16:54
  • @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. Commented Feb 7, 2014 at 8:28

1 Answer 1

1

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
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.