0

I need some help, I can create the records just fine; but I cannot access specific records within the project array. How can I access a specific record within the projects array. I've tried a number of ways but nothing gets displayed. I'm using Mongoose, with express, passport, and mongo.

var userSchema = new mongoose.Schema({
email: { type: String, unique: true, lowercase: true },
password: String,

profile: {
name: { type: String, default: '' },
location: { type: String, default: '' }
},
project:
  [{
    test: String,
    test1: String,
  }],
resetPasswordToken: String,
resetPasswordExpires: Date
});
Damian Buttle
  • 75
  • 3
  • 11

1 Answers1

1

If you want to find the whole document then you can use mongoose's find method. Like userSchema.findById({ _id: req.params.id }) as the documents that are stored in MongoDB always creates it unique id so that you can access those specific documents.

And if you want to find only specific data from that object then you can use this.

userSchema.findOne({ _id: id }, { project: { $elemMatch: { test: abc } } })

here only the project will be shown which has test=abc so that you can update the project. In the first object, it matches the object and selects only those objects inside the array of the project which has test=abc.

findOne will give you only first object that matches if you need all use find.

click here to see the documentation of elemMatch

Suresh Shetiar
  • 147
  • 1
  • 12