Mongoose update the field of the nested document not working - node.js

I've tried to update the field of the nested sub document using mongoose.
Here are the model and source code.
Model
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var info_schema = mongoose.schema({
info:Schema.Types.Mixed,
},{collection:'info'});
var InfoModel = mongoose.model('info', info_schema);
I've tried to execute db.info.updateMany({'info.addition.group':myid},{$set:{'info.addition.field1':'a','info.addition.field2':'b'}}) in mongoshell.
It worked well,but it didn't work using mongoose.
InfoModel.updateMany({'info.addition.group':myid},{$set:{'info.addition.field1':'a','info.addition.field2':'b'}}).exec();
Why doesn't mongoose update the field of the sub nested document?
So I tried to described the model in more detail.
var info_schema = mongoose.schema({
info:{
addition:Schema.Types.Mixed,
otherinfo:String,
modified:Number,
....
},
},{collection:'info'});
At this time , mongoose threw out the error.
CastError: Cast to number failed for value "a" at path "addition"
What did I do incorrectly?
What is the reason of this?

This works using your code and modifying it a little bit:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var info_schema = new Schema({ // <-- just use Schema
info: Schema.Types.Mixed
},{collection:'info'});
var InfoModel = mongoose.model('info', info_schema);
InfoModel.updateMany({'info.addition.group': 1}, // <-- I used 1 for testing
{ $set: { 'info.addition.field1' : 'a', 'info.addition.field2' : 'b' } }).exec()
Tested locally after first creating a record with info.addition.id == 1 so that I can update it etc.

Related

Imported mongoose model cannot perform operations

I have created a mongoose model in a file User.js:
var mongoose = require('mongoose');
exports.GetUser=function(conn){
var UserSchema = mongoose.Schema({
username: String,
age: Number
},{collection:"User",versionKey: false});
var usermodal = conn.modal("User",UserSchema);
return usermodal;
}
I am importing and using it in test.js file like this:
var user = require('./User.js');
var mongoose = require('mongoose');
var conn = mongoose.createConnection(\\connection string and other config here);
var userModal = user.GetUser(conn);
userModal.find({},(err, result)=>{
console.log(result); //prints undefined
});
The result comes undefined here. When I moved the model inside test.js, it started working and fetched data from the collection correctly. I cannot find what is the issue here. There are other models as well in User.js which I am exporting and using in the same way but they are also not working.
Please help me to find the issue and correct it. Thanks!

How do I update the existing documents' schema?

I'm using mongoose to do some MongoDB operations.
At the beginning the category was number,
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const sampleSchema = new Schema({
category: {
type: Number,
}
})
module.exports = mongoose.model("SampleSchema", sampleSchema);
Now the category changed to String, So I changed the model like this
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const sampleSchema = new Schema({
category: {
type: String,
}
})
module.exports = mongoose.model("SampleSchema", sampleSchema);
The problem is, I have already inserted 200 records into this collection. Is there any way to update the category value with a string and change its type to string?
Please get All data by query and update it one by one in loop.
Like:
db.tableName.find( { 'status' : { $type : 1 } } ).forEach( function (val) {
val.status = new String(val.status);
db.tableName.save(val);
});
I changed the category to mixed, that's working fine with numbers and string.
Thanks for the help #prasad_

Querying result from mongoose using dynamic model.find

I need to find the results of a query with mongoose find({}) method in Node.js with a variable containing model name.
var adSchema = new Schema({ schema defination });
var Ad = mongoose.model('Ad', adSchema);
var variableName = 'Ad';
variableName.find({}).exec(function (err, adObj) {});
Is it possible or not?
Thanks in advance
You should be able to do that when calling model with just the name like so
mongoose.model('Ad').find({}).exec(function (err, adObj) {});
See here for the corresponding part of the official docs
Try this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var anySchema = new Schema({
fieldname: String
});
var Test = mongoose.model('Test', anySchema);
Test.find({}).exec(function(err,result){});

Accessing subdocument properties in mongoose

I am using a MEAN stack to build this application.
Here is my subject.js schema:
var mongoose = require('mongoose');
var schema = mongoose.Schema;
var topics = require('./topic');
var subjectSchema = new schema({
_category : {
type: String,
default: ""
},
topics: [topics.schema]
});
module.exports = mongoose.model('Subject', subjectSchema);
and my topics.js schema:
var mongoose = require('mongoose');
var schema = mongoose.Schema;
var otherstuff = require('./otherstuff');
var otherstuff2 = require('./otherstuff2');
var topicSchema = new schema ({
title: String,
otherstuff: [mongoose.model('otherstuff').schema],
otherstuff2: [mongoose.model('otherstuff2').schema]
});
module.exports = mongoose.model('Topic', topicSchema);
What I am having difficulty with is how to access my topicSchema to populate it with forms from my front end.
I can save information to the subjectSchema, but not the sub documents.
I have tried using this as outlined in another article:
var Subject = mongoose.model('Subject', subjectSchema);
Subject.find({}).populate('subjects[0].topics[0].title').exec(function(err, subjects) {
console.log(subjects[0].topics[0].title);
});
But I continue to get TypeError: Cannot read property 'title' of undefined. How do I access the title property?
populate in mongoose is used to populate referenced documents, that are marked with ref attribute (see more info in the docs). Sub-documents on the other hand are available when do a simple query because they are actually an array of custom objects, so if you remove the populate method your query will work as expected:
Subject.find({}).exec(function(err, subjects) {
console.log(subjects[0].topics[0].title);
});

Mongoose: Generate Schema from existing data

For example, I have user collection in db:
{'_id':0, 'name': 'Joe', 'score':[80, 33]}
{'_id':1, 'name': 'Moe', 'score':[90, 81]}
... ...
How can I read this data with existing format, which means, use it's existing schema without create a new one.
I read Mongoose doc and googled for a while but didn't find satisfied answer.
When using mongoose with an existing collection, you have to reference the collection in your mongoose schema. The way to do this is to add the name of the collection to your schema. So if your collection is in 'mongodb://localhost:27017/test' and called 'things', you would use:
~~~
const Schema = mongoose.Schema;
const ThingSchema = new Schema({
name: {
type: String
}
});
const Model = mongoose.model('Thing', ThingSchema, 'things');
module.exports = Model;
~~~
Thanks to http://chrisflx.blogspot.fr/2014/04/nodejs-mongoose-with-preexisting-data.html?m=1
It will work if you make a model with the same schema.
var schema = new mongoose.Schema({ name: 'string', score: [] });
var user = mongoose.model('User', schema);
Edit:
Mongoose is an ODM, so you need a schema to create objects. If you need to run queries and get raw data from the database, I would stick with this library:
https://github.com/mongodb/node-mongodb-native

Resources