Mongodb populate query in nodejs for having condition with populated field - node.js

bellow is my schema's for User, Task and Order. i want to have query which can give me all orders which is assign to particular User.
var User = mongoose.Schema({
firstName : {
type: String,
required: false
},
lastName : {
type: String,
required: false
},
password : {
type: String,
required: false
},
createdAt:{
type: Date,
require: false
},
role:{
type: String,
require: false
}
});
var Task = mongoose.Schema({
assignedOn : {
type:Date
},
assignedTo : {type:mongoose.Schema.Types.ObjectId, ref:'User'},
status : {
type:String
},
priority : {
type: String
},
completeBy:{
type: Date
}
});
var Order = mongoose.Schema({
orderDate:{
type: Date,
require: false
},
completionDate:{
type: Date,
require: false
},
status: {
type: String,
required: false
},
orderType: {
type: String,
required: false
},
remarks: {
type: String,
require: false
},
totalAmount: {
type: Number,
require: false
},
items: {
type: [],
require: true
},
billingAddress: {
type: IAddress,
require: true
},
shippingAddress: {
type: IAddress,
require: true
},
defaultTask: {type:mongoose.Schema.Types.ObjectId, ref:'Task'},
customer: {type:mongoose.Schema.Types.ObjectId, ref:'Customer'}
});
i am doing query like bellow which is not giving me proper result for specific user _id which i want , its giving me all Orders.
this._orderRepository.findAndPopulate({$match:{assignedTo:_id}},
{path:'defaultTask ', populate:{path:'items'}, populate:{path:'assignedTo' }}, function(err, result){
console.log("error =>"+err);
/*console.log("result=>"+result);*/
callback(err, result);
} )

Related

How to get data from mongoDB on the basis of a value inside a nested object

Below is my API for fetching all the orders of a specific restaurant from a mongoDB database.
My search is based on the restaurant id which I am getting from params and passing it to the query object.
query1 is working but query2 does not return my data.
It is returning an empty array. If I pass a simple object in find() then I am getting a response. But when i use a nested object for getting data I get nothing.
exports.getOrders = async (req, res) => {
const restId = req.params.restId;
console.log("restaurant id", restId);
if (!restId) return res.status(404).send("No restaurant ID found in params");
const query1 = {grandTotal: 600};
const query2 = { restaurant: { restaurantId: restId } };
const response = await Orders.find(query2);
console.log("Printing response inside API function", response);
}
Below is my MongoDB Schema of Orders.
const mongoose = require("mongoose");
const orderSchema = mongoose.Schema({
customer: {
name: {
type: String,
required: true,
},
contact: {
type: String,
required: true,
},
customerId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Customer",
required: true,
},
customerAddress: {
type: String,
required: true,
},
},
restaurant: {
restaurantName: {
type: String,
required: true,
},
contact: {
type: String,
required: true,
},
restaurantId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Restaurant",
required: true,
},
},
/*date: {
day: {
type: Number,
required: true,
},
month: {
type: String,
required: true,
},
year: {
type: Number,
required: true,
},
},
type: {
type: String,
enum: ["delivery","takeaway"],
required: true,
},*/
items: [
{
itemId: { type: String, required: true },
itemName: { type: String, required: true },
itemDescription: { type: String, required: true },
price: {type: Number, required: true},
quantity: { type: Number, required: true },
total: {type: Number, required: true},
},
],
grandTotal: {type: Number, required: true},
status: {
type: String,
enum: ["pending", "rejected","cancelled","accepted","received"],
required: true,
},
});
orderSchema.virtual("booking", {
ref: "Booking",
localField: "_id",
foreignField: "orderId",
});
orderSchema.set("toObject", { virtual: true });
orderSchema.set("toJSON", { virtual: true });
const Orders = mongoose.model("Orders", orderSchema);
exports.Orders = Orders;
change query2 to this
const query2 = { "restaurant.restaurantId": restId } };

Mongoose/MongoDB Empty SubSchema Array

I have a model in Mongoose that looks like this:
const orderLogSchema = mongoose.Schema(
{
removed: { type: String, required: true },
},
{
timestamps: true,
}
)
const orderSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
orderItems: [
{
name: { type: String, required: true},
qty: { type: Number, required: true},
image: { type: String, required: true},
price: { type: Number, required: true},
product: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Product'},
}
],
shippingAddress: {
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: String, required: true },
country: { type: String, required: true },
},
paymentMethod: {
type: String,
required: true,
},
paymentResult: {
id: { type: String },
status: { type: String },
update_time: { type: String },
email_address: { type: String },
},
taxPrice: {
type: Number,
required: true,
default: 0.0
},
shippingPrice: {
type: Number,
required: true,
default: 0.0
},
totalPrice: {
type: Number,
required: true,
default: 0.0
},
isPaid: {
type: Boolean,
required: true,
default: false
},
paidAt: {
type: Date,
},
isDelivered: {
type: Boolean,
required: true,
default: false
},
deliveredAt: {
type: Date,
},
couponCode: {
type: Object,
required: false
},
orderVerifyLog: [orderLogSchema],
}, {
timestamps: true
})
I need to be able to empty out the orderVerifyLog which is populated with the orderLogSchema. I have tried a bunch of things most recently I pulled the order into a variable, and I am able to access orderVerifyLog by using order.orderVerifyLog but I cant figure out how to empty out that orderLogSchema. I tried:
order.orderVerifyLog = []
order.save()
Is there an easy way to work with that "sub" schema and zero it out?
Basically I am pulling the log items from that schema for an order and displaying them on my front-end. Then I was to fire off an action to clears it out so they don't display every time the order is loaded (Only want them to show once). I also tired to loop over each item in the orderVerifyLog and use pull to remove them, but for some reason it always leaves one in there.

MongoD: How to update specific field of a document in a collection?

I have a collection of jobs in MongoDB and in this collection there is a field in the documents named as appliedUser. I want to update this field when a new user applies for this job.So basically this field stores the id's of all the users who are applying for this job.
I am using findOneAndUpdate() function but not able to do it.
Job.findOneAndUpdate({ _id: req.params.id }, { $set: { appliedUser:
req.user.id } }, function(err, job) {
console.log(job);
})
and here is my Schema:
const jobSchema = new Schema({
date: { type: Date, default: Date() },
expirydate: { type: Date, required: true },
name: { type: String, required: true },
companydetails: { type: String, required: true },
address: { type: String, required: true },
role: { type: String, required: true },
city: { type: String, required: true },
minsalary: { type: Number, required: true },
maxsalary: { type: Number, required: true },
skills: { type: Array, required: true },
minex: { type: Number, required: true },
appliedUser: [{ type: Schema.Types.ObjectId, ref: 'users', unique:
true }],
user: { type: String, required: true }
})
The array of the document is not updating. I am not able to find the errors.
Look like what you need is $addToSet. Example:
Job.findOneAndUpdate({ _id: req.params.id }, { $addToSet: { appliedUser: req.user.id } }, function(err, job) {
console.log(job);
})

find according to referenced objectid

i have this schema:
var scheme = schema({
name: { type: String, required: true },
deleted: { type: Boolean, default: false },
user: {
type: ObjectId,
ref: 'User'
},
created: {type: Date, default: Date.now},
modified: {type: Date, default: Date.now},
eventinfo: { type: String, required: false },
street: { type: String, required: false },
city: { type: String, required: false },
country: { type: String, required: false },
postalcode: { type: String, required: false },
usecurrentlocation: { type: Boolean, required: false },
schedule_agenda_html: { type: String, required: false },
gspsetup_html: { type: String, required: false },
evdropboxstat: { type: Boolean, required: false },
evsponsors_html: { type: String, required: false },
evexhibitors_html: { type: String, required: false },
floorplan_html: { type: String, required: false },
social_media_fb: { type: String, required: false },
social_media_tw: { type: String, required: false },
social_media_li: { type: String, required: false },
evorganizers_html: { type: String, required: false },
coverscreen_img: { type: String, required: false },
logo_img: { type: String, required: false },
background_img: { type: String, required: false },
background_color: { type: String, required: false },
event_fromdate: {type: Date, default: Date.now},
event_todate: {type: Date, default: Date.now},
attendees_feedback: { type: Boolean, required: false },
social_network: { type: Boolean, required: false },
feature_icon_color: { type: String, required: false }
});
exports.EacEvent = mongoose.model("EacEvent", scheme);
i need to find all the EacEvents where user _id is equal to some thing like this (56a8a1eaf7a3289c10c9ed30) i have tried many things but either i am getting all the EacEvents or no EacEvent. thanks
Route File
here is my route file code which i am using to get the required results..
var models = require('../models')
,mongoose = require('mongoose')
var schema = mongoose.Schema;
var ObjectId = schema.ObjectId;
// var ObjectID = schema.ObjectId;
exports.myeventapps = function (req, res) {
var eac_userid;
if (req.user){
eac_userid = JSON.stringify(req.user._id);
console.log('================================' + eac_userid);
}
//models.EacEvent.find().exec(function(err, myevents) {
models.EacEvent.find({ deleted: false,'user._id':ObjectId(eac_userid)}).sort({ modified: -1 }).exec(function(err, myevents) {
res.locals.myevents = myevents;
console.log('================================' + myevents);
res.render('eac_myevent_apps', { title: "My Event Apps", myeventapps: myevents });
});
}
i have also tried comparing user._id in .populate() function but did not get the required results
Please try it, match user field directly, rather than user._id.
Refer to Mongoose Populate.
models.EacEvent.find({ deleted: false, 'user': ObjectId(eac_userid)}).sort(...
Maybe we can use the req.user._id directly
models.EacEvent.find({ deleted: false, 'user': req.user._id}).sort(...

mongoosejs findByIdAndUpdate a best way

I'm wondering if there a best way to do this:
/**
* Article Schema
*/
var PostSchema = new Schema({
title: {
type: String,
required: true,
trim: true
},
author:{
type: String,
required: true,
default: 'whisher'
},
slug: {
type: String,
index: { unique: true }
},
body: {
type: String,
required: true,
trim: true
},
avatar:{
type: String,
required: true
},
status: {
type: String,
required: true,
trim: true
},
created: {
type: Date,
required: true,
default: Date.now
},
published: {
type: Date,
required: true
},
categories: {
type: [String]
},
tags: {
type: [String],
required: true,
index: true
},
comment: {
type: Schema.Types.ObjectId,
ref: 'CommentSchema'
},
meta: {
votes: {
type: Number,
default: 0
},
comments: {
type: Number,
default: 0
}
}
});
/**
* Comment Schema
*/
var CommentSchema = new Schema({
post_id: {
type: Schema.Types.ObjectId,
ref: 'Post',
required: true
},
author:{
type: String,
required: true
},
email:{
type: String,
required: true
},
web:{
type: String
},
body: {
type: String,
required: true,
trim: true
},
status: {
type: String,
required: true,
default: 'pending'
},
created: {
type: Date,
required: true,
default: Date.now
},
meta: {
votes: Number
}
});
/**
* Create a comment
*/
exports.create = function(req, res) {
var comment = new Comment(req.body);
comment.save(function(err) {
if (err) {
return res.jsonp(500,{ error: 'Cannot save the comment' });
}
Post.findById(comment.post_id).exec(function(err, post) {
if (err) {
return res.jsonp(404,{ error: 'Failed to load post with id ' + comment.post_id });
}
if (!post) {
return res.jsonp(404,{ error: 'Failed to load post with id ' + comment.post_id });
}
post.meta.comments = post.meta.comments++;
post.save(function(err) {
if (err) {
return res.jsonp(500,{ error: 'Cannot update the post' });
}
res.jsonp(200,comment);
});
});
});
};
Btw I just looking at http://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate
but like this:
Model.findByIdAndUpdate(comment.post_id, { post.meta.comments: post.meta.comments++ })
doesnt work
I think you need to use the $inc operator to increment the comment count like this...
Post.findByIdAndUpdate(comment.post_id, { $inc: {"meta.comments" : 1} }, callback);

Resources