0

i have all user data which is inserted in mongodb.

[
{

    "user_id": "1",
    "name": "aaaa",
    "value": -10
  }, {

    "user_id": "2",
    "name": "bbbb",
    "value": 30

  }, {
    ,
    "user_id": "1",
    "name": "aaaa",
    "value": 310

  }, {

    "user_id": "2",
    "name": "bbbb",
    "value": -15
  }, {

    "user_id": "1",
    "name": "aaaa",
    "value": -100

  }, {

    "user_id": "3",
    "name": "cccc",
    "value": 390

  }]

how to do query for my requirment? in my document some user contaning value in negative . for example user_id = 1 contaning two negative value object i need to find out using query how can i modify my code?

User.findOne({user_id: '1'},function(err, user) { 
    if (err) return next(err);
    if (!user) return res.status(401).send('Unauthorized');
    res.json(user);
    });
its me
  • 620
  • 5
  • 23
  • 48

3 Answers3

1

To get multiple document use find instead of findOne. When you use find you get the output as array of objects. You can try the below code

User.find({user_id: '1' ,value : {$lt : 0}},function(err, users) { 
    if (err) return next(err);
    res.json(users);
});
azhar
  • 1,591
  • 1
  • 16
  • 36
1

If you just need to find all entries with negative values, just specify value < 0 as your query criteria:

User.find({value : {$lt : 0}},function(err, users) { 
  if (err) return next(err);
  if (!users || users.length) {
    return res.status(401).send('Unauthorized');
  }
  res.json(users);
});
Ben
  • 4,757
  • 2
  • 15
  • 22
0

Try this

By referring your sample schema, it is array of documents of user_id's

Use $unwind to seperate the docs from array and filter the documents by using given user_id and whose value is negative.

Sample document

{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : [
                {
                        "user_id" : "1",
                        "name" : "aaaa",
                        "value" : -10
                },
                {
                        "user_id" : "2",
                        "name" : "bbbb",
                        "value" : 30
                },
                {
                        "user_id" : "1",
                        "name" : "aaaa",
                        "value" : 310
                },
                {
                        "user_id" : "2",
                        "name" : "bbbb",
                        "value" : -15
                },
                {
                        "user_id" : "1",
                        "name" : "aaaa",
                        "value" : -100
                },
                {
                        "user_id" : "3",
                        "name" : "cccc",
                        "value" : 390
                }
        ]
}

db.collection.aggregate([{$unwind:"userArray"}]);

The result of unwind on above data would be

{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "1",
                "name" : "aaaa",
                "value" : -10
        }
}
{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "2",
                "name" : "bbbb",
                "value" : 30
        }
}
{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "1",
                "name" : "aaaa",
                "value" : 310
        }
}
{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "2",
                "name" : "bbbb",
                "value" : -15
        }
}
{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "1",
                "name" : "aaaa",
                "value" : -100
        }
}
{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "3",
                "name" : "cccc",
                "value" : 390
        }
}

Then apply the condition of that is user_id and value is negative. For this we can use $match together with $and (since we have two conditions value and id)

db.collection.aggregate([{$unwind:"$userArray"}, {$match:{$and: [{"userArray.value":{$lte:0}}, {"userArray.user_id":"1"}]}} ]);

for user_id: 1 the query result is

{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "1",
                "name" : "aaaa",
                "value" : -10
        }
}
{
        "_id" : ObjectId("582032556772b7cb0ff85721"),
        "userArray" : {
                "user_id" : "1",
                "name" : "aaaa",
                "value" : -100
        }
}

With this result you have to iterate it in your service layer and work on the other desired results.

Hope it helps!

Clement Amarnath
  • 4,779
  • 1
  • 16
  • 29