Home > Blockchain >  mongoose: how to find all data with querying array of object?
mongoose: how to find all data with querying array of object?

Time:10-16

I have a document structure like this:

{
    "_id": "",
    "personalities": [
        {
            "personalName": "something",
            "color":0,
            "total":0,
            "userId":""
        }
    ]
}

I tried to find with this method:

User.find({personalities:{
        $$elemMatch:{
            personalName:"something"
        }
    }})
        .then(users => res.status(200).send(JSON.stringify(users)))
        .catch(err => res.status(400).send(err))

Edit: code {"personalities.personalName":"something"} work on mongoDbCompass. enter image description here

but when i query with mongoose. it return all of docs.

app.post("/users", (req, res) => {
    User.find({"personalities.personalName":"something"})
        .then(users => res.status(200).send(JSON.stringify(users.length)))
        .catch(err => res.status(400).send(err))
})

but it returns all documents (even documents with personalName not equal "something"). how can I fix it?

I really appreciate your help and hope you have a nice day!

CodePudding user response:

The perfect method is to use findById method. Just pass the user id and get all its relevant details like below.

User.findById(userId).then(user=> //do something here).catch(err=> console.log(err)

However, if you don't have Id you can get the single user by matching the user details using findOne method. You can find reference here https://mongoosejs.com/docs/api.html#model_Model.findOne.

User.findOne({  personalName:"something" }).then(user=> //do something).catch(err=>console.log(err)

Further, there is possibility that there are multiple records with matching where clause, in that case (and better) you should go for find Method. It works something like below.

// find all documents
await MyModel.find({});

// find all documents named john and at least 18
await MyModel.find({ name: 'john', age: { $gte: 18 } }).exec();

// executes, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});

// executes, name LIKE john and only selecting the "name" and "friends" fields
await MyModel.find({ name: /john/i }, 'name friends').exec();

// passing options
await MyModel.find({ name: /john/i }, null, { skip: 10 }).exec();

Last but not least, if there is array inside the document and you want to search by targeting objects inside the array then we can easily accomplish this.

User.find(
    { "personalities.personalName": "something"  }
    function(err, result) {
      if (err) {
        res.send(err);
      } else {
        res.json(result);
      }
    }
  );

or something like below with callback

User.find({ "personalities.personalName": "something"  `})
.then(users=> //do something with users).catch(error=>console.log(error))`

CodePudding user response:

As in your current attempt, $elemMatch is used. I take an assumption of you need to find all documents where there is one matched entry of "personalName": "something" in the personalities array.

A simple find should suffice.

db.collection.find({
  "personalities.personalName": "something"
})

Here is the Mongo playground for your reference.

  • Related