I want to populate the category field in product schema.
The code for my product schema is
const mongoose = require('mongoose');
const {ObjectId} = mongoose.Schema
const productSchema = new mongoose.Schema({
name :{
type:String,
required : true,
trim : true,
maxlength : 32,
},
description : {
type:String,
required : true,
trim : true,
maxlength : 2000,
},
price : {
type : Number,
required : true,
maxlength :32
},
category : {
type : ObjectId,
ref : "Category",
required : true
},
stock : {
type : Number,
},
sold : {
type :Number,
default : 0
},
photo : {
data : Buffer,
contentType : String
}
},{timestamps:true})
module.exports = mongoose.model("Product",productSchema);
And The code which i am using to populate the category field is
exports.getAllProducts = (req,res)=>{
let no_of_products = req.query.limit? parseInt(req.query.limit):8;
let sortBy = req.query.sortBy?req.query.sortBy : "price"
Product.find()
.populate("catgeory")
.select("-photo")
.sort({sortBy:1})
.limit(no_of_products)
.exec((err,products)=>{
if(err){
console.log(err);
return res.status(400).json({
error : "some error occured"
})
}
})
}
But this code is giving me error which say
cannot populate path Catgeory because it is not in your schema. Set the strictPopulate option to false to override.
I have read documentation but i am unable to alter my code . Kindly help me with this issue.
check the Product databases table that you have category keys and you should know that the object Id is correct and it comes from the category table if not you should drop the database or drop the product collection and create the new one
I've got an Express application which until this morning was returning an array of objects from the database. Now it's returning an empty array. RoboMongo shows me that the data is still there and doing fine. Any ideas?
My model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const plotStatusSchema = new Schema(
{
recordDate: Date,
blockName: String,
growerName: String,
company: String,
variety: String,
planted: Number,
region: String,
yieldInKG: Number,
changeInPcnt: Number,
currentRipeness: String,
nextStage: String,
timeToNextInDays: Number,
status: Number
},
{ bufferCommands: false },
{ collection: 'plotStatuses' }
);
const ModelClass = mongoose.model(
'plotStatus',
plotStatusSchema,
'plotStatuses'
);
module.exports = ModelClass;
My returning controller:
const PlotStatus = require('../models/plotStatus');
const jsonpack = require('jsonpack');
exports.plotStatuses = async (req, res) => {
const plotStatus = await PlotStatus.find({
company: 'req.user.companyCode'
}).lean();
if (!plotStatus) {
throw new Error('Plot Statuses not found');
} else {
res.send(plotStatus);
}
};
A sample of my data:
{
"_id" : ObjectId,
"recordDate" : ISODate,
"blockName" : String,
"blockCode" : String,
"growerName" : String,
"company" : String,
"variety" : String,
"planted" : ISODate,
"region" : String,
"yieldInKG" : Number,
"changeInPcnt" : Number,
"currentRipeness" : String,
"nextStage" :String,
"timeToNextInDays" : Number,
"status" : Number,
"targetYieldInKG" : Number,
"currentStatePercentage" : Number,
"totalLengthOfPhase" : Number,
"nextPhaseStart" : ISODate,
"currentBrix" : Number,
"currentPh" : Number,
"currentTA" : Number,
"plotGeoJSON" : Object,
"historicalData" : Array
}
I know that the Schema no longer matches the shape of the JSON, but can I return all the JSONs that fit the find condition anyway?
You are searching the string "req.user.companyCode" inside the company attribute. Obviously, you don't have that company code in your data. Try it again without the quores:
const plotStatus = await PlotStatus.find({
company: req.user.companyCode
}).lean();
I am new at MongoDB. I am trying to create 2 db's :
1 - Dogs
2 - Person
I want a person to have 0, 1 or more dogs, and a partner (who is also a person)
tried to do:
var dogSchema = new mongoose.Schema({
name: String,
age: Number,
breed: String
});
var Dog = mongoose.model("Dog", dogSchema);
var personSchema = new mongoose.Schema({
name: String,
age: Number,
partner: this,
dogs: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Dog"
}
]
});
var Person = mongoose.model("Person", personSchema);
But when I try to create a new person, things getting complicated.
what is the best way to do this?
This code work fine for me:
"use strict"
const mongoose = require('mongoose');
const categorySchema = mongoose.Schema({
name: String,
parent: {type:mongoose.Schema.Types.ObjectId, ref:"Category"},
subcategory: {type:mongoose.Schema.Types.ObjectId, ref:"Category"},
});
const Category = mongoose.model('Category', categorySchema);
async function run() {
await mongoose.connect("mongodb://localhost:27017/mongoose-test");
const category = new Category({name:"Name"});
const subcategory = new Category({name:"Name 2", parent:category});
category.subcategory = subcategory;
await Promise.all([category.save(), subcategory.save()]);
};
run();
Mongo:
> db.categories.find();
{ "_id" : ObjectId("59f4a8f32ebdb940cc2bed3a"), "subcategory" :
ObjectId("59f4a8f32ebdb940cc2bed3b"), "name" : "Name", "__v" : 0 }
{ "_id" : ObjectId("59f4a8f32ebdb940cc2bed3b"), "name" : "Name 2",
"parent" : ObjectId("59f4a8f32ebdb940cc2bed3a"), "__v" : 0 }
Lemme take time to explain what is happening from start to finish.
Preamble:
A user a follows 10 other people. When user A logs in, an X number of posts from each of the 10 people are pulled into view.
I do not know if it is the right thing to do, and will appreciate a better way of doing it. However, I wanna give it a try, and it ain't working.
Follow Model:
let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let FollowSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
followers: [{
type: Schema.Types.ObjectId,
ref: 'Card'
}],
following: [{
type: Schema.Types.ObjectId,
ref: 'Card'
}]
});
module.exports = mongoose.model('Follow', FollowSchema);
Card Model
let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let CardSchema = new Schema({
title: String,
content: String,
createdById: {
type: Schema.Types.ObjectId,
ref: 'User'
},
createdBy: {
type: String
}
});
module.exports = mongoose.model('Card', CardSchema);
Follow logic
When user A follows user B, do two things:
Push the user_id of B to user A document on field 'following' (A is following B)
Push user_id of A to user B document on field 'followers' (B is followed by A)
router.post('/follow', utils.loginRequired, function(req, res) {
const user_id = req.user._id;
const follow = req.body.follow_id;
let bulk = Follow.collection.initializeUnorderedBulkOp();
bulk.find({ 'user': Types.ObjectId(user_id) }).upsert().updateOne({
$addToSet: {
following: Types.ObjectId(follow)
}
});
bulk.find({ 'user': Types.ObjectId(follow) }).upsert().updateOne({
$addToSet: {
followers: Types.ObjectId(user_id)
}
})
bulk.execute(function(err, doc) {
if (err) {
return res.json({
'state': false,
'msg': err
})
}
res.json({
'state': true,
'msg': 'Followed'
})
})
})
Actual DB values
> db.follows.find().pretty()
{
"_id" : ObjectId("59e3e27dace1f14e0a70862d"),
"user" : ObjectId("59e2194177cae833894c9956"),
"following" : [
ObjectId("59e3e618ace1f14e0a708713")
]
}
{
"_id" : ObjectId("59e3e27dace1f14e0a70862e"),
"user" : ObjectId("59e13b2dca5652efc4ca2cf5"),
"followers" : [
ObjectId("59e2194177cae833894c9956"),
ObjectId("59e13b2d27cfed535928c0e7"),
ObjectId("59e3e617149f0a3f1281e849")
]
}
{
"_id" : ObjectId("59e3e71face1f14e0a708770"),
"user" : ObjectId("59e13b2d27cfed535928c0e7"),
"following" : [
ObjectId("59e3e618ace1f14e0a708713"),
ObjectId("59e13b2dca5652efc4ca2cf5"),
ObjectId("59e21942ca5652efc4ca30ab")
]
}
{
"_id" : ObjectId("59e3e71face1f14e0a708771"),
"user" : ObjectId("59e3e618ace1f14e0a708713"),
"followers" : [
ObjectId("59e13b2d27cfed535928c0e7"),
ObjectId("59e2194177cae833894c9956")
]
}
{
"_id" : ObjectId("59e3e72bace1f14e0a708779"),
"user" : ObjectId("59e21942ca5652efc4ca30ab"),
"followers" : [
ObjectId("59e13b2d27cfed535928c0e7"),
ObjectId("59e2194177cae833894c9956"),
ObjectId("59e3e617149f0a3f1281e849")
]
}
{
"_id" : ObjectId("59f0eef155ee5a5897e1a66d"),
"user" : ObjectId("59e3e617149f0a3f1281e849"),
"following" : [
ObjectId("59e21942ca5652efc4ca30ab"),
ObjectId("59e13b2dca5652efc4ca2cf5")
]
}
>
With the above database results, this is my query:
Query
router.get('/follow/list', utils.loginRequired, function(req, res) {
const user_id = req.user._id;
Follow.findOne({ 'user': Types.ObjectId(user_id) })
.populate('following')
.exec(function(err, doc) {
if (err) {
return res.json({
'state': false,
'msg': err
})
};
console.log(doc.username);
res.json({
'state': true,
'msg': 'Follow list',
'doc': doc
})
})
});
With the above query, from my little understanding of Mongoose populate, I expect to get cards from each of the Users in the following array.
My understanding and expectations might be wrong, however with such an endgoal, is this populate approach okay? Or am I trying to solve an aggregation task with population?
UPDATE:
Thanks for the answer. Getting quite close, but still, the followingCards array contains no result. Here's the contents of my current Follow model:
> db.follows.find().pretty()
{
"_id" : ObjectId("59f24c0555ee5a5897e1b23d"),
"user" : ObjectId("59f24bda1d048d1edad4bda8"),
"following" : [
ObjectId("59f24b3a55ee5a5897e1b1ec"),
ObjectId("59f24bda55ee5a5897e1b22c")
]
}
{
"_id" : ObjectId("59f24c0555ee5a5897e1b23e"),
"user" : ObjectId("59f24b3a55ee5a5897e1b1ec"),
"followers" : [
ObjectId("59f24bda1d048d1edad4bda8")
]
}
{
"_id" : ObjectId("59f24c8855ee5a5897e1b292"),
"user" : ObjectId("59f24bda55ee5a5897e1b22c"),
"followers" : [
ObjectId("59f24bda1d048d1edad4bda8")
]
}
>
Here are all the current content I have from Card Model:
> db.cards.find().pretty()
{
"_id" : ObjectId("59f24bc01d048d1edad4bda6"),
"title" : "A day or two with Hubtel's HTTP API",
"content" : "a day or two",
"external" : "",
"slug" : "a-day-or-two-with-hubtels-http-api-df77056d",
"createdBy" : "seanmavley",
"createdById" : ObjectId("59f24b391d048d1edad4bda5"),
"createdAt" : ISODate("2017-10-26T20:55:28.293Z"),
"__v" : 0
}
{
"_id" : ObjectId("59f24c5f1d048d1edad4bda9"),
"title" : "US couple stole goods worth $1.2m from Amazon",
"content" : "for what",
"external" : "https://bbc.com",
"slug" : "us-couple-stole-goods-worth-dollar12m-from-amazon-49b0a524",
"createdBy" : "nkansahrexford",
"createdById" : ObjectId("59f24bda1d048d1edad4bda8"),
"createdAt" : ISODate("2017-10-26T20:58:07.793Z"),
"__v" : 0
}
With the Populate Virtual example from yours (#Veeram), here's the response I get:
{"state":true,"msg":"Follow list","doc":{"_id":"59f24c0555ee5a5897e1b23d","user":"59f24bda1d048d1edad4bda8","following":["59f24b3a55ee5a5897e1b1ec","59f24bda55ee5a5897e1b22c"],"followers":[],"id":"59f24c0555ee5a5897e1b23d","followingCards":[]}}
The followingCards array is empty.
Using the $lookup query on the other hand simply returns []
I'm likely missing something?
You can use either virtual populate or $lookup operator in aggregation pipeline.
Using Virtual Populate
FollowSchema.virtual('followingCards', {
ref: 'Card',
localField: 'following',
foreignField: 'createdById'
});
Follow.findOne({
'user': Types.ObjectId(user_id) })
.populate('followingCards')
.exec(function(err, doc) {
console.log(JSON.stringify(doc));
});
Using $lookup aggregation
Follow.aggregate([
{
"$match": {
"user": Types.ObjectId(user_id)
}
},
{
"$lookup": {
"from": "cards",
"localField": "following",
"foreignField": "createdById",
"as": "followingCards"
}
}
]).exec(function (err, doc) {
console.log(JSON.stringify(doc));
})
var mongoose = require('mongoose'), Schema = mongoose.Schema
var eventSchema = Schema({
title : String,
location : String,
startDate : Date,
endDate : Date
});
var personSchema = Schema({
firstname: String,
lastname: String,
email: String,
dob: Date,
city: String,
eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }]
});
var Event = mongoose.model('Event', eventSchema);
var Person = mongoose.model('Person', personSchema);
To show how populate is used, first create a person object,
aaron = new Person({firstname: 'Aaron'}) and an event object,
event1 = new Event({title: 'Hackathon', location: 'foo'}):
aaron.eventsAttended.push(event1);
aaron.save(callback);
Then, when you make your query, you can populate references like this:
Person
.findOne({ firstname: 'Aaron' })
.populate('eventsAttended') .exec(function(err, person) {
if (err) return handleError(err);
console.log(person);
});
// only works if we pushed refs to person.eventsAttended
note: change Activity.find to Card.find
const { ObjectID } = require("mongodb");
// import Follow and Activity(Card) schema
const userId = req.tokenData.userId; // edit this too...
Follow.aggregate([
{
$match: {
user: ObjectID(userId)
}
}
])
.then(data => {
// console.log(data)
var dataUsers = data[0].following.map(function(item) {
return item._id;
});
// console.log(dataUsers)
Activity.find(
{ createdById: { $in: dataUsers } },
{
_id: 1,
title: 1,
content: 1,
createdBy: 1,
creatorAvatar: 1,
activityType: 1,
createdAt: 1
}
)
// .sort({createdAt:-1)
.then(posts => res.send({ posts }));
});
I've written the following MongooseJS schema.The field "likers" is an embedded record which currently is a POJO with two keys. Can the embedded schema be a hash map with each "liker" field serving as the key?
Here's what the hash map should look like :
const userlikers = {
{'100900800' : 'user'}
{'100989455' : 'user'}
{'109099985' : 'user'}
}
Here's the current schema
const userlikers = Schema({
users : {
type: Schema.Types.Mixed
}
})
const PicSchema = Schema ({
url : {
type : String,
required : true
},
desc : {
type : String,
required : true
},
likers : [userlikers],
created : {
by : {
type : Schema.Types.ObjectId,
ref : 'User',
required : true
},
at : {
type : Date,
default : Date.now
}
}
})
const Pic = mongoose.model('Pic', PicSchema);
module.exports = Pic;
I know it is so late,
From mongoose version 5.10.9 you can use the map instead of type: Schema.Types.Mixed
const PicSchema = Schema ({
..............
........
likers : {
type: Map,
of: String
},
........
........
})