0

In my webservice I was passing array of strings and then I was parsing it like this:

var users = req.body.users;

if (users) {
   var user = users.split(',');
   query.$and.push({"device_id": {$nin: user}});
}

but now my requirement changed and instead of strings I'm passing array like this:

"users": (
{
    "device_id" = "XXX";
    "display_name" = XXX;
    "facebook_username" = XXX;
    username = XXX;
},
{
    "device_id" = "YYY";
    "display_name" = YYY;
    "facebook_username" = YYY;
    username = YYY;
}
)

like before - I want to filter out all records that contain given device_ids. How can I modify my code so that it iterates over array (and take into consideration specific fields) instead of strings?

1 Answer 1

1

Assuming that req.body.users is an array try the following:

var users = req.body.users;
var excludedDeviceIds = [ ];

for(var i in users) {
    excludedDeviceIds.push(users[i]['device_id']);
}

query.$and.push({ 'device_id': { $nin: excludedDeviceIds } });

Edit:

If your environment supports ES6 you can also try:

query.$and.push({ 
    'device_id': { 
        $nin: users.map(user => user['device_id']) 
    } 
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much, it's all clear now! Unfortunately I had to rewrite (mostly expand) my functionality and now I face different problem, I set up additional question here stackoverflow.com/questions/41071134/… could you take a look? maybe that's sth you would know about, thanks!

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.