Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 309 Vote(s) - 3.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mongoose, find, return specific properties

#1
I have this get call:

exports.getBIMFromProject = function(req, res){
mongoose.model('bim').find({projectId: req.params['prj_id']}, function(err, bim){
if(err){
console.error(err);
res.send(500)
}
res.send(200, bim);
});
};

Where do I specify which properties I want to return? Can't find it in the docs. The above returns the entire object. I only want a few properties returned.

This is my schema:

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var bimSchema = new Schema({
projectId: Number,
user: String,
items:[
{
bimObjectId: Number,
typeId: String,
position:{
floor: String,
room:{
name: String,
number: String
}
}
}
]
});

mongoose.model('bim', bimSchema);

I don't want the items array included in my rest call.

Reply

#2
You use projection. The first example in the [mongoose query docs](

[To see links please register here]

) has a projection operation tucked in.

NB: not real code b/c I highlighted the important bits with triple stars

// find each person with a last name matching 'Ghost', ***selecting the `name` and `occupation` fields***
Person.findOne({ 'name.last': 'Ghost' }, ***'name occupation'***, function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.
})

The `Person` schema isn't specified but I think the example is clear enough.
Reply

#3
`.Select()` method is used to select which fields are to be returned in the query result
```
let result = await MyModel.find({ user : "user" }).select('name lastname status')
```
Reply

#4
With the help of ```.select()``` this is possible.
- If number of fields required are less from total fields then,
```.select('projectId user')``` can be used
- Else number of fields to be ignored are less from total fields then,
```.select('-items')``` can be used.

So for getting a field, simply, space separated string of fields can be passed and for ignoring the field, space separated string with "-" before the field can be used.

For [more documentation][1].


[1]:

[To see links please register here]

Reply

#5
Mongoose provides multiple ways to project documents with [`find`][3], [`findOne`][2], and [`findById`][1].

### 1. Projection as String:

```javascript
// INCLUDE SPECIFIC FIELDS
// find user and return only name and phone fields
User.findOne({ email: email }, 'name phone');

// EXCLUDE SPECIFIC FIELD
// find user and return all fields except password
User.findOne({ email: email }, '-password');
```

### 2. Projection by passing `projection` property:

```javascript
// find user and return just _id field
User.findOne({ email: email }, {
projection: { _id: 1 }
});
```

### 3. Using [`.select`][4] method:

```javascript
// find user and return just _id and name field
User.findOne({ email: email }).select('name');

// find user and return all fields except _id
User.findOne({ email: email }).select({ _id: 0 });
```

You can do the same with [`find`][3] and [`findById`][1] methods too.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply

#6
Find Specific properties also avoid some properties
```
await User.find({ email: email }, { name: 1, phone: 1, status: 1, password: 0 });
```
Reply

#7
MyModel.find({name: "john"}, 'name age address', function(err, docs) {
console.log(docs);
});
This will return fields - `name`, `age` and `address` only.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through