Here is my code to get one floorplan and populate all flats linked with this
see the code below :
var floorplan = Floorplan.find({
project: req.params.project,
tower: req.params.tower,
isDeleted: false
});
floorplan.populate('flats').exec(function(err, floorplan) {
if (err) { return res.send(err); }
if (!floorplan) { return res.status(401).json(); }
res.status(200).json(floorplan);
});
But I want to populate only those flats where isDeleted : false
How to achive this ??
Schema of floorplan
var FloorplanSchema = new Schema({
project: { type: Schema.ObjectId, ref: "Project" },
flats: [{ type: Schema.ObjectId, ref: "Flat" }],
tower: [{ type: Schema.ObjectId, ref: "Tower" }],
unitType: String,
area: Number,
floorPlan2D: String,
floorPlan3D: String,
livingRoomArea: Number,
kitchenArea: Number,
balconies: Number,
bathRooms: Number,
isDeleted: { type: Boolean, 'default': false },
createdAt: { type: Date, 'default': Date.now }
});
Schema of flat
var FlatSchema = new Schema({
tower: { type: Schema.ObjectId, ref: "Tower" },
floorplan: { type: Schema.ObjectId, ref: "Floorplan" },
project: { type: Schema.ObjectId, ref: "Project" },
status: String,
floor: Number,
size: String,
superbuiltup_area: Number,
directionFacing: String,
furnishingState: String,
flooringType: String,
createdAt: { type: Date, 'default': Date.now },
isDeleted: { type: Boolean, 'default': false },
});
The populate() method has an option which allows for filtering, you can either try this
Floorplan
.find({
project: req.params.project,
tower: req.params.tower,
isDeleted: false
})
.populate({
path: 'flats',
match: { isDeleted: false }
})
.exec(function(err, floorplan) {
if (err) { return res.send(err); }
if (!floorplan) { return res.status(401).json(); }
res.status(200).json(floorplan);
});
or
Floorplan
.find({
project: req.params.project,
tower: req.params.tower,
isDeleted: false
})
.populate('flats', null, { isDeleted: false })
.exec(function(err, floorplan) {
if (err) { return res.send(err); }
if (!floorplan) { return res.status(401).json(); }
res.status(200).json(floorplan);
});
Related
I am trying to populate user informations to get his location but i am getting undefined.
Here are my schemas:
AnnounceSchema:
const AnnounceSchema = new mongoose.Schema({
titre: String,
contenu: String,
image: String,
tag: String,
media: String,
date: {
type: Date,
default: Date.now
},
commentaires: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
authorId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
})
UserSchema:
const UserSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
email: { type: String, required: true, unique: true },
loc: { type: { type: String, default: 'Point' }, coordinates: { type: [Number] } }
});
UserSchema.index({ loc: '2dsphere' });
And here is my query :
Announce.find({})
.populate('authorId', null,
{
'authorId.loc': {
$near: {
$geometry: {
type: 'Point',
coordinates: [req.user.loc.coordinates[0], req.user.loc.coordinates[1]]
},
$maxDistance: 10000
}
}
})
.exec((err, announces) => {
if (err) {
res.send(err)
} else {
res.render('announce/search', { announces: announces })
}
})
The error I'm getting is "unable to find index for $geoNear query". I've added an index to the AnnonceSchema but no change:
AnnonceSchema.index({ "authorId.loc" : "2dsphere"})
This should work:
Announce.find({})
.populate('author',
{
path : 'authorId',
match: {
'loc': {
$near: {
$geometry: {
type: 'Point',
coordinates: [req.user.loc.coordinates[0], req.user.loc.coordinates[1]]
},
$maxDistance: 10000
}
}
},
model: 'User',
})
.exec((err, announces) => {
if (err) {
res.send(err)
} else {
res.render('announce/search', { announces: announces })
}
})
populate works in two ways
it can populate by reference Announce.find({}).populate('authorId')
or by a custom query (which is what you need)
My schema is as shown below:
const order = new Schema({
order_status: Number,
foodtruck_id: { type: Schema.Types.ObjectId, ref: 'foodtruck' },
customer_id: { type: Schema.Types.ObjectId, ref: 'user' },
items: [{ type: Schema.Types.ObjectId, ref: 'items' }],
user_type: Boolean,
order_time: Date,
order_rating: { type: Number, default: 5.0 },
order_issue_comments: String,
order_special_instruction: String,
order_total: Number,
order_location: String,
order_coupon_code: String,
payment_id: { type: Schema.Types.ObjectId, ref: 'payment' },
order_meta: { type: Schema.Types.Mixed, ref: 'order_sub_info', default: {} }
}, { versionKey: false }, { minimize: false });
my query is as shown below:
order.find({
'foodtruck_id': foodtruck_id.trim()
}).populate('customer_id', {
'_id': 1,
'user_name': 1,
'email_id': 1,
'ph_no': 1,
'login_type': 1
}).populate('items').
populate('order_meta', 'order_otp').exec((err, orderList) => {
if (err) res.json({
status: '500',
message: err
});
else {
console.log("called");
res.json({
status: '200',
message: 'Order list',
data: orderList
});
}
});
For this query,it is giving me Cast to ObjectId failed for value at path _id as order_meta has default value {}. How to have effective populate query so that It can take care of this testcase?
It is not good idea to put empty object in a place, where reference id is expected. Both - for having problem with populate and for common sense too (if it is field which has reference, it should be null/undefined or reference itself).
It is common that you want to transform your data at some endpoint, but it should not interfere with database or business logic of application.
You can defined toJSON method that should be used for your model. In your case
const order = new Schema({
order_status: Number,
foodtruck_id: { type: Schema.Types.ObjectId, ref: 'foodtruck' },
customer_id: { type: Schema.Types.ObjectId, ref: 'user' },
items: [{ type: Schema.Types.ObjectId, ref: 'items' }],
user_type: Boolean,
order_time: Date,
order_rating: { type: Number, default: 5.0 },
order_issue_comments: String,
order_special_instruction: String,
order_total: Number,
order_location: String,
order_coupon_code: String,
payment_id: { type: Schema.Types.ObjectId, ref: 'payment' },
order_meta: { type: Schema.Types.ObjectId, ref: 'order_sub_info'}
}, { versionKey: false }, { minimize: false });
order.options.toJSON = {
transform(zipRequestDocument, ret, options) { // eslint-disable-line no-unused-vars
if (!ret.order_meta){
ret.order_meta = {};
}
},
};
Everytime I hit api I am getting same error-
I have tried sending value as parameters also but failed. Any help would be appreciated. When i use $set it updates same value everytime the web service is called but it does work but not with $push.
MongoError: The field 'songId' must be an array but is of type objectId in document
{_id: ObjectId('59709590380026118c22dd61')}
My Playlist schema code:-
var PlaylistSchema = new mongoose.Schema({
name: String,
coverphoto: { type: String, default: '' },
userId: {
type: ObjectId,
ref: 'User'
},
songId: [{ type: ObjectId, ref: 'Song' }],
updated_at: { type: Date, default: Date.now },
});
my Song Schema code
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var SongSchema = new mongoose.Schema({
audioName:{type:String,default:''},
title: String,
// coverphoto: { type: String, default: '' },
singer: { type: String, default: '' },
movie: { type: String, default: '' },
album: { type: String, default: '' },
lyricist: { type: String, default: '' },
actors: { type: String, default: '' },
lyrics: { type: String, default: '' },
genre: { type: String, default: 'Random' },
duration: { type: String, default: '' },
size: { type: String, default: '' },
userId: {
type: ObjectId,
ref: 'User'
},
categoryId: {
type: ObjectId,
ref: 'Category'
},
updated_at: { type: Date, default: Date.now },
});
module.exports = mongoose.model('Song', SongSchema);
My Api code
/* post api to add song in playlist */
router.post('/addSongToPlaylist', function (req, res, next) {
Playlist.findOne({ '_id': req.body.playlistId }, function (err, playlist) {
if (err) return next(err);
console.log(playlist)
console.log("req.body.songId", req.body.songId);
if (playlist) {
Playlist.findByIdAndUpdate(
req.body.playlistId,
{ $push: { "songId": req.body.songId } },
{ new: true },
function (err, playlistData) {
if (err) return next(err);
res.json({ message: 'New Song added successfully', playlist: playlistData, success: true });
});
} else if (!Song) {
res.json({ message: 'Failed to add song', success: false });
}
});
});
I am building a search query where it will find a database object by its ID even while the user is typing.
ordersRouter.route('/searchorder/:term')
.get(function(req, res){
term = req.params.term;
console.log(term);
Orders.findById(term)
.populate({ path: 'userPurchased products.product', select: '-username -password' })
.exec(function(err, orders){
if (err) throw err;
res.json([orders]);
});
});
The problem here is that when the term does not exactly the same as the ID, it will return nothing. How can I return IDs with partial term?
EDIT: My order model schema
var orderSchema = new Schema({
orderId: { type: String },
userPurchased: { type: Schema.Types.ObjectId, ref: 'users' },
products: [
{
product: { type: Schema.Types.ObjectId, ref: 'products' },
size: { type: String, required: true },
quantity: { type: Number, required: true },
subTotal: { type: Number, required: true }
}
],
totalQuantity: { type: Number },
totalPrice: { type: Number },
modeOfPayment: { type: String },
shippingAd: { type: String },
refNumber: { type: String },
isDone: { type: Boolean, default: false },
orderStatus: { type: String, default: 'Pending' },
dateOrdered: { type: Date, default: Date.now() },
fromNow: { type: String }
});
You can run your query using regex i.e. create a regular expression object from the string using the RegExp constructor then run the query as:
ordersRouter.route('/searchorder/:term')
.get(function(req, res){
term = req.params.term;
console.log(term);
Orders.find({'orderId': new RegExp(term)})
.populate({
path: 'userPurchased products.product',
select: '-username -password'
})
.exec(function(err, orders){
if (err) throw err;
res.json(orders);
});
});
I'm having real problems with population on the below schemas. It may not be the best design of models (relatively new to MEAN stacks) but I can get it to populate everything except the Spec model.
// Spec Model
var SpecSchema = new Schema({
time: {
type: Date,
default: Date.now
},
active: {
type: Boolean,
default: 'true'
},
name: String,
desc: String
});
module.exports = mongoose.model('Spec', SpecSchema);
// Thing model
var specsSchema = new Schema({
time: {
type: Date,
default: Date.now
},
spec: {
type: Schema.Types.ObjectId,
ref: 'Spec'
},
value: String,
});
var ThingSchema = new Schema({
time: {
type: Date,
default: Date.now
},
active: {
type: Boolean,
default: true
},
title: String,
specs: [specsSchema]
});
var Thing = mongoose.model('Thing', ThingSchema);
// Set model
var thingsSchema = new Schema({
time: {
type: Date,
default: Date.now
},
active: {
type: Boolean,
default: 'true'
},
thing: {
type: Schema.Types.ObjectId,
ref: 'Thing'
}
});
var SetSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
time: {
type: Date,
default: Date.now
},
active: {
type: Boolean,
default: 'true'
},
title: String,
things: [thingsSchema]
});
var Set = mongoose.model('Set', SetSchema);
The standard population is fine but i cant for the life of me get the model.populate to work and from all the examples and solutions I have looked at I'm unclear as to what the path should be.
Set.findById(req.params.id)
.populate('things.thing')
.populate('user', '_id name')
.exec(function (err, set) {
if(err) { return handleError(res, err); }
if(!set) { return res.send(404); }
Thing.populate(set,{
path:'things.thing.specs.spec',
select: 'name',
model: Spec
}, function(err, set){
if ( err ) return res.json(400, err);
});
return res.json(set);
});
any pointers in the right direction would be much appreciated.
path:'things.thing.specs.spec',
select: 'name',
model: Spec
should be
path:'things.thing.specs.spec',
select: 'name',
model: 'Spec'