I have 2 mongoose Models/Schemas (Products & Merchants). When doing a .find() on Products it is returning all products, as expected but when doing it for Merchants it is returning everything (both merchant and product data) when it should just be returning merchants based off the defined schema. I have one collection on my mongoDB server which contains all my merchants and the products.
Any idea what I am missing?
Thanks
Merchants Model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var MerchantShema = new Schema({
merchants: [{
merchant_id: Number,
price_current: Number,
price_rrp: Number,
aff_link: String,
merchant_product_id: Number,
aw_image_url: String,
cost_scoop: String,
created_at: Date,
updated_at: Date
}]
});
module.exports = mongoose.model('Merchants', MerchantShema, 'products');
Products Model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ProductSchema = new Schema({
ean: Number,
aw_product_id: Number,
product_name: String,
product_brand: String,
product_description: String,
img_sml: String,
img_lrg: String,
cat: String,
sub_cat: String,
weight: String,
rating: String,
created_at: Date,
updated_at: Date,
merchants: [{
merchant_id: Number,
price_current: Number,
price_rrp: Number,
aff_link: String,
merchant_product_id: Number,
aw_image_url: String,
cost_scoop: String,
created_at: Date,
updated_at: Date
}],
nutrition: [{
calories: Number,
protein: Number,
fat: Number,
sat_fat: Number,
carbs: Number,
sugar: Number,
salt: Number,
calories: Number
}],
ingredients: String,
flavours: String,
is_active: Boolean
});
module.exports = mongoose.model('Products', ProductSchema, 'products');
My Routes
app.get('/api/products', function(req, res) {
Products.find(function(err, products){
if (err) return console.error(err);
return res.send(products);
})
.where('is_active').equals('true')
});
app.get('/api/merchants', function(req, res){
Merchants.find(function(err, merchants){
if (err) return console.error(err);
return res.send(merchants);
});
});
Example of my collection
[
{
"_id": "55840f86e4b0ba19c15ee26d",
"merchant_id": "1111",
"merchant_aw_id": "1",
"merchant_name": "test merchant",
"merchant_url": "google.com",
"merchant_image": "",
"created_at": "",
"updated_at": "",
"merchants": []
},
{
"_id": "558426f9e4b0ba19c15ee495",
"ean": "123456789",
"aw_product_id": "55555",
"product_name": "Test Product",
"product_brand": "Test Brand",
"product_description": "This is a description for the test product",
"img_sml": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
"img_lrg": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
"cat": "Protein",
"sub_cat": "Protein",
"weight": "2.5kg",
"rating": "5",
"created_at": "",
"updated_at": "",
"nutrition": [
{
"salt": "1",
"sugar": "1",
"carbs": "1",
"sat_fat": "1",
"fat": "1",
"protein": "1",
"calories": "1"
}
],
"ingredients": "",
"flavours": "",
"is_active": "true",
"merchants": [
{
"merchant_id": 1111,
"price_current": 9.99,
"price_rrp": 15.99,
"aff_link": "google.com",
"merchant_product_id": 999,
"aw_image_url": "",
"cost_scoop": "43p",
"created_at": "",
"updated_at": ""
}
]
},
{
"ean": "123456789",
"aw_product_id": "55555",
"product_name": "Test Product",
"product_brand": "Test Brand",
"product_description": "This is a description for the test product",
"img_sml": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
"img_lrg": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
"cat": "Protein",
"sub_cat": "Protein",
"weight": "2.5kg",
"created_at": "",
"updated_at": "",
"nutrition": [
{
"salt": "1",
"sugar": "1",
"carbs": "1",
"sat_fat": "1",
"fat": "1",
"protein": "1",
"calories": "1"
}
],
"ingredients": "",
"flavours": "",
"is_active": "true",
"merchants": [
{
"merchant_id": 1111,
"price_current": 9.99,
"price_rrp": 15.99,
"aff_link": "google.com",
"merchant_product_id": 999,
"aw_image_url": "",
"cost_scoop": "43p",
"rating": "5",
"created_at": "",
"updated_at": ""
}
]
}
]
In looking at your merchants model definition. You are calling products in your export:
module.exports = mongoose.model('Merchants', MerchantShema, 'products');
This should probably read
module.exports = mongoose.model('Merchants', MerchantShema, 'merchants');
Another thing I noticed is that you have two keys called "calories".
I ran the code and it now brings back merchants correctly. However, when you call products, it brings back the merchant as part of that schema, since merchants are included in products. Another way you can do this is to use a nested schema. You can read about those in this other thread: Mongoose subdocuments vs nested schema
Hope that helps.
Related
This question already has answers here:
Mongoose/Mongodb: Exclude fields from populated query data
(4 answers)
Closed 2 years ago.
I just not want to pass user id in discussion array.
Now I getting back from this route like this.
{
"_id": "5f4600ab7ec81f6c20f8608d",
"name": "2",
"category": "2",
"description": "2",
"deadline": "2020-08-10",
"discussion": [
{
"date": "2020-09-03T06:12:15.881Z",
"_id": "5f5089bd265ec85b896f8491",
"user": {
"_id": "5f5089a2265ec85b896f848f",
"userName": "MdJahidHasan01"
},
"text": "3"
},
{
"date": "2020-09-03T06:12:15.881Z",
"_id": "5f5089ae265ec85b896f8490",
"user": {
"_id": "5f5089a2265ec85b896f848f",
"userName": "MdJahidHasan01"
},
"text": "2"
}
]
}
But I want to get like this
{
"_id": "5f4600ab7ec81f6c20f8608d",
"name": "2",
"category": "2",
"description": "2",
"deadline": "2020-08-10",
"discussion": [
{
"date": "2020-09-03T06:12:15.881Z",
"_id": "5f5089bd265ec85b896f8491",
"user": {
"userName": "MdJahidHasan01"
},
"text": "3"
},
{
"date": "2020-09-03T06:12:15.881Z",
"_id": "5f5089ae265ec85b896f8490",
"user": {
"userName": "MdJahidHasan01"
},
"text": "2"
}
]
}
Select does not working here. I just not want to pass user id in discussion array just username.
As I use user id for authorization. So it is not an good idea to send user id.
Project Model
const mongoose = require('mongoose');
const projectSchema = new mongoose.Schema({
name: {
type: String,
require: true
},
category: {
type: String,
require: true
},
description: {
type: String,
require: true
},
deadline: {
type: String,
require: true
},
discussion: [
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
date: {
type: Date,
default: Date.now()
},
text: {
type: String,
require: true
}
}
]
});
module.exports = mongoose.model('Project', projectSchema);
Project Details Route
router.get('/:projectId',async (req, res) => {
try {
const project = await Project.findById(req.params.projectId)
.populate('discussion.user', 'userName')
.select('-discussion.user._id')
console.log(project);
await res.status(200).json(project);
} catch (error) {
console.log(error);
return res.status(400).json({ 'error': 'Server Error' });
}
})
Just add this after the .populate:
delete project.discussion._id
I have two schemas: 'Leads' and 'LeadsCategory'.
Leads Schema:
const id = mongoose.Schema.Types.ObjectId;
const leadsSchema = mongoose.Schema(
{
_id: id,
userId: { type: id, ref: "User", required: true },
leadName: String,
leads: [
{
_id: id,
name: String,
mobile: Number,
address: String,
education: {
school: String,
graduation: String
},
leadType: { type: id, ref: "LeadsCategory", required: true }
}
]
},
{
timestamps: true
}
);
module.exports = mongoose.model("lead", leadsSchema);
Leads Category Schema:
const id = mongoose.Schema.Types.ObjectId;
const leadsCategorySchema = mongoose.Schema({
_id: id,
name: {
type: String,
required: true
},
leadsData: [{ type: id, ref: 'lead' }]
},
{ timestamps: true }
);
module.exports = mongoose.model("LeadsCategory", leadsCategorySchema);
I'm referencing the leadsCategory as soon as new lead is created and it does populate my leadsCategory into the Leads controller.
So my final data inside 'Leads collection' looks like this:
[
{
"_id": "5e8832dde5d8273824d86502",
"leadName": "Freshers",
"leads": [
{
"education": {
"school": "LPS",
"graduation": "some school"
},
"location": {
"state": "delhi",
"country": "india"
},
"name": "Joey",
"mobile": 1524524678,
"_id": "5e8832dde5d8273824d86500",
"leadType": {
"_id": "5e88285f5dda5321bcc045a6",
"name": "all"
}
},
{
"education": {
"school": "DAV",
"graduation": "some school"
},
"location": {
"state": "delhi",
"country": "india"
},
"name": "Ben",
"mobile": 1524524678,
"_id": "5e8832dde5d8273824d86501",
"leadType": {
"_id": "5e88285f5dda5321bcc045a6",
"name": "all"
}
}
]
}
]
But now I need to associate the leads data into my 'leadsCategory' collection so that I can query the leads data according to the leadType created. For now, I have only one 'leadType':'all'. But further, I will create more types and populate the data accordingly.
I tried something like this:
exports.get_leads_type_all = (req, res) => {
LeadsCategory.find()
.populate('leadsData')
.then( data => {
res.json(data)
})
}
But this returns me only empty array like this:
{ "leadsData": [],
"_id": "5e88285f5dda5321bcc045a6",
"name": "all",
"createdAt": "2020-04-04T06:25:35.171Z",
"updatedAt": "2020-04-04T06:25:35.171Z",
"__v": 0
},
Please help me to associate and related this data. I have tried lot's of thins but could not make it work.
try this:
exports.get_leads_type_all = (req, res) => {
LeadsCategory.find()
.populate('leadsData')
.execPopulate()
.then( data => {
res.json(data)
})
}
https://mongoosejs.com/docs/api/document.html#document_Document-execPopulate
This question already has an answer here:
Recursive elements in Schema : Mongoose modelling
(1 answer)
Closed 3 years ago.
I have a comment list Objects which stores the comments and whoever replies to that comment it getting store in the children
{
"_id": "5dbc479babc1c22683b73cf3",
"comment": "wow .. this is awsome",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "second child",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "hi darling",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
},
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "yep",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb9683b44bfa2a3dce55bd",
"username": "mayank",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/alxndrustinov/128.jpg"
},
"createdDate": "2019-11-01T14:56:27.580Z",
"updatedDate": "2019-11-01T14:58:38.188Z",
"__v": 0
}
and here is the schema which i have designed
var mongoose = require('mongoose');
let UserSchema = new mongoose.Schema({
username:{
type:String
},
avatar:{
type:String
}
});
var ChildrenSchema = new mongoose.Schema({
"comment":{
type:String
},
parentId:{
type: mongoose.Schema.Types.ObjectId,
},
children:{
type:[ChildrenSchema]
},
user:{
type:UserSchema
}
},{timestamps: { createdAt: 'createdDate', updatedAt: 'updatedDate' }});
let CommentSchema = new mongoose.Schema({
user:{
type:UserSchema,
},
"comment":{
type:String
},
children:{
type:[ChildrenSchema]
}
},{timestamps: { createdAt: 'createdDate', updatedAt: 'updatedDate' }});
var Comment = mongoose.model('Comment', CommentSchema);
module.exports = {Comment};
here I am trying to give children type as the same ChildrenSchema but it is not working it is throwing { CastError: Cast to embedded failed for value "{ comment: \'hi darling\',\n children: [],\n user:\n { _id: \'5dbb81c8c597533bf4c38e75\',\n username: \'arunkavale\',\n avatar:\n \'https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg\' } }" at path "children error .. I am not getting how to do this .please help me in this
Modify the way of defining Subdocuments array as below and give a try
children:[ChildrenSchema]
const mongoose = require('mongoose');
const addressSchema = mongoose.Schema({
type: String,
street: String,
city: String,
state: String,
country: String,
postalcode: Number
});
const createStudentSchema = mongoose.Schema({
admisionnum: Number,
first: String,
last: String,
phone: String,
address : [addressSchema],
isActive : { type : Boolean, default: true}
}, {
timestamps: true
});
module.exports = mongoose.model('createStudent', createStudentSchema);
Addreschema for address in array of objects . I am getting null in the output for the json below .
This is json input
{
"admisionnum":"1",
"first": "Dan",
"last": "HEllo",
"phone": "9000909575",
"address" : [
{
"type": "own",
"street": "18 ksdhks",
"city": "chennai",
"state": "Tamil Nadhu",
"country": "India",
"postalcode": "600097"
}
],
"isActive": "true"
}
Response Json output
{
"isActive": true,
"_id": "5c9e57f718e3de2ca4dd1d86",
"admisionnum": 1,
"first": "Jesus",
"last": "christ",
"phone": "9000909575",
"address": [
null,
null,
null,
null,
null,
null
],
"createdAt": "2019-03-29T17:37:59.291Z",
"updatedAt": "2019-03-29T17:37:59.291Z",
"__v": 0
}
Please help how to bind data for address . controller code is below
exports.createStudent = (req, res) => {
// Create a Note
const CreateStudent = new createStudent({
admisionnum: req.body.admisionnum,
first: req.body.first,
last: req.body.last,
phone: req.body.phone,
address : [
req.body.address.type,
req.body.address.street,
req.body.address.city,
req.body.address.state,
req.body.address.country,
req.body.address.postalcode
] ,
isActive: req.body.type
});
// Save Note in the database
CreateStudent.save()
.then(data => {
res.send(data);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while creating the Note."
});
});
};
Just use address : req.body.address, and it should work fine.
Since your address is an array and you are sending address array as well in the request, you don't need to do anything else.
Try this:
const CreateStudent = new createStudent({
admisionnum: req.body.admisionnum,
first: req.body.first,
last: req.body.last,
phone: req.body.phone,
address : req.body.address ,
isActive: req.body.type
});
I have following model
var reservationSchema = new Schema({
lcode: Number,
reserved_days: [daySchema],
});
var daySchema = new Schema({
pk: Number,
date: String,
price: Number,
payment_type: String,
payment_date: String,
room: { room_id: String, name: String }
});
I have a reservation with array of reserved_days. I need to compare this reservation and reservations in database and find reservation where at least one reserved_days date or reserved_days room.room_id are the same that contains in array. Array must not be fully equal, I just need to find duplicate days.
Now I try the following
Reservation.find({
$or: [{
"reserved_days.room.room_id": {
$in: req.body.reservation.reserved_days
}
}, {
"reserved_days.date": {
$in: req.body.reservation.reserved_days
}
}]
}).exec(function(err, found)
but it doesn't work.
Test request reservation.reserved_days object looks the following
"reserved_days": [
{
"pk": 3543,
"date": "2018-07-14",
"price": 3213.0,
"payment_type": "Безналичный расчет",
"payment_date": "2017-12-28",
"room": {
"room_id": "7",
"name": "Стандарт"
}
},
{
"pk": 3544,
"date": "2018-07-15",
"price": 3213.0,
"payment_type": "Безналичный расчет",
"payment_date": "2017-12-28",
"room": {
"room_id": "7",
"name": "Стандарт"
}
}]
Is it possible to do ?