Prevent _id from being populated by just one schema - mongodb

So I have the following schema:
var List = new Schema(
{
item_details_template: {
default: [
{
"title": "Name",
"content": "",
"hidden": false,
"order": 0,
"the_type": "text"
},
{
"title": "Price",
"content": "",
"hidden": false,
"order": 1,
"the_type": "text"
},
{
"title": "Company",
"content": "",
"hidden": false,
"order": 2,
"the_type": "text"
}
],
type: [Item_Detail]
}
}
)
However, I don't want ONLY this schema (subdocument) to not create _id fields. How do I do this? I know you can change the original schema itself, but it's being used by other Schemas, where I would like the _id to be populated.

To suppress _id on the item_detail_template, you need to restructure the way you create subdocument as follows
var mongoose = require("mongoose");
var subSchema = mongoose.Schema({
//your subschema content
},{ _id : false });
var schema = mongoose.Schema({
// schema content
subSchemaCollection : [subSchema] // item_details_template
});
var model = mongoose.model('tablename', schema);

If you want to suppress _id on the item_detail_template in the List schema only:
var List = new Schema(
{
item_details_template: {
_id:false, // should supress _id property
default: [
...

var widgetSchema = new Schema({ ... attributes ... }, { versionKey: false });

Related

how to dynamically change whole object (inc. array) in mongodb?

I use following code to dynamically update whole object in mongoDB:
module.exports = function(req, res, next){
const Model = require('../models/' + req.body.where)
for(let i=0; i<req.body.array.length; i++){
Model.updateOne({
"_id": req.body.array[i]._id,
"user_ID": req.body.user_ID
},{
$set: req.body.array[i]
}).catch(next)
if(i+1 == req.body.array.length) res.send({})
}
}
but the code is not working, when model own array:
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const exerciseSchema = new Schema({
title: {
type: String,
required: [true, 'required!']
}
}, { _id : false })
const workout_planSchema = new Schema({
title: {
type: String,
required: [true, 'required!']
},
description: {
type: String
},
user_ID: {
type: String,
required: [true, 'required!']
},
exercises: [exerciseSchema]
})
const workout_plan = mongoose.model('workout_plan', workout_planSchema)
module.exports = workout_plan
I would like to update whole object with totally new values, staying only with the same _id.
For example , I have following value in DB:
"_id": "604a16f6cf847c1810c8fd08",
"title": "1",
"user_ID": "Test",
"exercises": [{
"title": "123"
}],
and I am sending array which looks like this:
"_id": "604a16f6cf847c1810c8fd08",
"title": "2",
"user_ID": "Test",
"exercises": [{
"title": "234"
},{
"title": "235"
}],
and the result should be same as the array I am sending. How can I change my code to reach this?
PS: Basiclly I want to make object in DB = sent object
so the answer is to change the main function like this:
module.exports = function(req, res, next){
const Model = require('../models/' + req.body.where)
for(let i=0; i<req.body.array.length; i++){
Model.replaceOne({
"_id": req.body.array[i]._id,
"user_ID": req.body.user_ID
},
req.body.array[i]
).catch(next)
if(i+1 == req.body.array.length) res.send({})
}
}
Try this one:
for( let elem of req.body.array ) {
let workout_plan = await Model.findById(elem._id);
workout_plan.title = elem.title;
workout_plan.user_ID = elem.user_ID;
workout_plan.exercises = elem.exercises;
await workout_plan.save();
}

Mongoose query not showing subdocument

I can't get mongoose to show subdocument when running find() while it displays perfectly well in mongodb shell.
Subdocument should be embedded based on my schema, not objectId referenced, so I shouldn't be running any black magic voodoo to get my data to show up.
const UserSchema = new mongoose.Schema({
username: String;
xp: Number;
//etc.
});
const RoomSchema = new mongoose.Schema({
timestamp: { type: Date, default: Date.now },
status: { type: String, enum: ["pending", "ongoing", "completed"]},
players: {
type: [{
points: { type: Number, default: 0 },
position: String,
user: UserSchema
}],
maxlength:2
}
});
After adding a new room with:
let room = new Room(coreObj);
room.players.push({
points: 0,
position: 'blue',
user: userObj //where userObj is a result of running findById on User model
});
It displays nicely in mongo shell, when running db.rooms.find({}).pretty() I can see that full document has been added. However, when running on mongoose model:
Room.find({}).exec((err,rooms)=>{
console.log(rooms[0].toJSON());
});
I don't see user subdocument, moreover I cannot see user field entirely! What seems to be the problem?
logged json from mongoose model:
{
"status": "pending",
"_id": "5cf5a25c050db208641a2076",
"timestamp": "2019-06-03T22:42:36.946Z",
"players": [
{
"points": 0,
"_id": "5cf5a25c050db208641a2077",
"position": "blue"
}
],
"__v": 0
}
json from mongo shell:
{
"_id" : ObjectId("5cf5a25c050db208641a2076"),
"status" : "pending",
"timestamp" : ISODate("2019-06-03T22:42:36.946Z"),
"players" : [
{
"points" : 0,
"_id" : ObjectId("5cf5a25c050db208641a2077"),
"position" : "blue",
"user" : {
"xp" : 0,
"_id" : ObjectId("5cf2da91a45db837b8061270"),
"username" : "bogdan_zvonko",
"__v" : 0
}
}
],
"__v" : 0
}
Keeping best practice in mind, I think it would be more appropriate to reference the UserSchema in the RoomSchema. Something like:
...
user: {
type: Schema.Types.ObjectId,
ref: 'UserSchema'
}
Then you would store the user._id in that field.
This way, if the user is modified, your RoomSchema is always referencing the correct information. You could then get the user using Mongoose's populate
I'm not entirely sure why you can't see the sub-sub-document, but this code example printed it correctly for me. Example was originally posted in https://mongoosejs.com/docs/subdocs.html but modified slightly to contain sub-sub-document so it looks similar to your code:
var grandChildSchema = new mongoose.Schema({ name: 'string' });
var childSchema = new mongoose.Schema({ name: 'string', grandChild: grandChildSchema });
var parentSchema = new mongoose.Schema({ children: [childSchema] });
var Parent = mongoose.model('Parent', parentSchema);
var parent = new Parent({
children: [
{ name: 'Matt', grandChild: {name: 'Matt Jr'} },
{ name: 'Sarah', grandChild: {name: 'Sarah Jr'} }
]
})
parent.save(function() {
Parent.find().exec(function(err, res) {
console.log(JSON.stringify(res[0]))
mongoose.connection.close()
})
});
Executing this code resulted in:
{
"_id": "5cf7096408b1f54151ef907c",
"children": [
{
"_id": "5cf7096408b1f54151ef907f",
"name": "Matt",
"grandChild": {
"_id": "5cf7096408b1f54151ef9080",
"name": "Matt Jr"
}
},
{
"_id": "5cf7096408b1f54151ef907d",
"name": "Sarah",
"grandChild": {
"_id": "5cf7096408b1f54151ef907e",
"name": "Sarah Jr"
}
}
],
"__v": 0
}
This was tested using Mongoose 5.5.12.
Note that I was using JSON.stringify() to print the document instead of using Mongoose's toJSON().
I just met a very similar problem, i think i got it.
the whole point is in model which you use:
const RoomSchema = new mongoose.Schema({
...
players: {
type: [{
...
user: UserSchema
...
but then you make
room.players.push({
points: 0,
position: 'blue',
user: userObj //where userObj is a result of running findById on User model
});
so you are missing the "type" subfield, so your doc is not compliant with your RoomSchema and mongoose do not show the parts which does not fit schema.

Mongoose virtual field for nested field returning null

My Schema looks like this, where associated is an array of String which are not _id and i use the Virtual method to populate another field in anoher Schema
const DetailSchema = mongoose.Schema({
candidate:{
.
.
associated: [String],
.
.
}
}, { toObject: { virtuals: true }, toJSON: { virtuals: true } });
My Virtual looks like
DetailSchema.virtual('associatedJobs', {
ref: 'Jobs',
localField: 'candidate.associated',
foreignField: 'jobID',
justOne: false
});
The returned field is always null. Is there something wrong?
Your reference to Jobs (ref: 'Jobs',), it maybe Job (ref: 'Job',) if you've declared your model is Job (without 's')
Your associatedJobs will be returned not in object, this is example, it maybe with format:
{
"candidate": {
"associated": [
"J2",
"J3",
"J5"
]
},
"_id": "5c1b4ab6683beb0b8162c80f",
"id": "D1",
"__v": 0,
"associatedJobs": [
{
"_id": "5c1b4ab6683beb0b8162c80b",
"jobID": "J2",
"name": "Job name 2",
"__v": 0
},
{
"_id": "5c1b4ab6683beb0b8162c80c",
"jobID": "J3",
"name": "Job name 3",
"__v": 0
},
{
"_id": "5c1b4ab6683beb0b8162c80e",
"jobID": "J5",
"name": "Job name 5",
"__v": 0
}
]
}
This is my solution for your problem, you can download on gist to run on local https://gist.github.com/huynhsamha/a728afc3f0010e49741ca627750585a0
My simple schemas as your schemas:
var DetailSchema = new Schema({
id: String,
candidate: {
associated: [String]
}
}, {
toObject: { virtuals: true },
toJSON: { virtuals: true }
});
var JobSchema = new Schema({
jobID: String,
name: String
});
DetailSchema.virtual('associatedJobs', {
ref: 'Job',
localField: 'candidate.associated',
foreignField: 'jobID',
justOne: false
});
var Detail = mongoose.model('Detail', DetailSchema);
var Job = mongoose.model('Job', JobSchema);
And you should add populate when find:
const d = await Detail.findOne({ id: 'D1' }).populate('associatedJobs');
You should mention both schema structure to find the error in your query ,I have post example of virtual field may be you get some help
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/test', { useMongoClient: true });
var PersonSchema = new mongoose.Schema({
name: String,
band: String
});
var BandSchema = new mongoose.Schema({
name: String
}, { toObject: { virtuals: true } });
BandSchema.virtual('members', {
ref: 'Person', // The model to use
localField: 'name', // Find people where `localField`
foreignField: 'band', // is equal to `foreignField`
// If `justOne` is true, 'members' will be a single doc as opposed to
// an array. `justOne` is false by default.
justOne: false
});
var Person = mongoose.model('Person', PersonSchema);
var Band = mongoose.model('Band', BandSchema);

Mongoose: Find subdocument from Mixed Type and update

I want to Update and delete sub document. The structure of document as follows,
{
"projectId": "12345-service-request",
"projectVersion": [{
"version":"1",
"localConfig": [{
"port": "3003",
"mongoURI": "mongodb://localhost:27017/serviceRequest",
"MQ": "RMQ",
"logLevel": "2",
"version": "1.1",
"created": "03-06-2018 03:11:00 PM",
"active": "N"
},
{
"port": "3004",
"mongoURI": "mongodb://localhost:27017/serviceRequest",
"MQ": "IMQ",
"logLevel": "1",
"version": "1.2",
"created": "07-06-2018 03:11:00 PM",
"active": "Y"
}]
}]
}
The fields in "localConfig" are not same, they are different as per project.
So I have created schema using Schema.Types.Mixed for this as below,
const mongoose = require('mongoose');
mongoose.set('debug', true);
const Schema = mongoose.Schema;
const projectVersion = new Schema({
version: {type:String, required: true},
cloudParams: [String],
localConfig: [{type:Schema.Types.Mixed}] // Mixed Type schema
});
const projectConfig = new Schema({
projectId : {type:String, required: true},
projectVersion : [projectVersion]
});
module.exports = mongoose.model("ProjectConfig", projectConfig);
Now, I am trying to find subdocument using condition,
projectId = 12345-service-request and
projectVersion.localConfig.version = 1.2
But is returns complete document.
Code to find sub document using $ and $elemMatch,
let projectId = '12345-service-request';
let version = '1.2'
// Using $
ProjectConfig.findOne({'projectId' : projectId,'projectVersion.localConfig.version' : version },
{ 'projectVersion.$' : 1 }).exec(function(err,projectConfig){
console.log("Result : "+JSON.stringify(projectConfig));
});
and
ProjectConfig.find({'projectId' : projectId,'projectVersion.localConfig.version' : version } ,
{ 'projectVersion.localConfig' : { $elemMatch : { version : version } } }).exec(function(err,projectConfig){
console.log("Result : "+JSON.stringify(projectConfig));
});
I am missing something or is there any other way to find subdocument from Mixed type.

How to set property of a model from a property of a `population` field in mongoose?

I have a Post and its author property is set to User by using ref. Suppose there is a country field in User, how to make Post schema to have a country property as well which value is from User.country after population.
const Post = new Schema({
text: String,
author: {
type: ObjectId,
ref: 'User'
},
// How to set the virtual property country which is from User.country?
});
I know there is a Populate Virtuals in Mongoose, but it seems it just another way of populating, it won't pick up one of the properties, but the whole referenced record. Or maybe I get it wrong?
I know I can refer the country from Post.author.country, but I want a Post.country as well.
How to solve this?
Could I do this at the schema level?
If you use Populate Virtuals you can do the following :
var schemaOptions = {
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
};
var PostSchema = new Schema({
text: String,
id: false,
author: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}, schemaOptions);
PostSchema.virtual('country').get(function() {
return this.author.country;
});
Using :
Post.find({}).populate('author').exec(function(error, data) {
console.log(JSON.stringify(data));
});
it gives :
[{
"_id": "59d924346be5702d16322a67",
"text": "some text",
"author": {
"_id": "59d91f1a06ecf429c8aae221",
"country": "France",
"__v": 0
},
"__v": 0,
"country": "France"
}]
Check this gist for a full example