mongoose

Mongoose findOneAndUpdate nested documents

允我心安 提交于 2021-02-08 04:47:40
问题 I'm trying to update a document which contains 2 other documents. To achieve this task, I use findOneAndUpdate , but mongoose throws me a CastError ( Cast to ObjectId failed ). My models: user.js var userSchema = new Schema({ email: String, info: { type : Schema.ObjectId, required : true, ref : 'Info' } }); module.exports = mongoose.model('User', userSchema); info.js var infoSchema = new Schema({ firstname: String, lastname: String }); module.exports = mongoose.model('Info', infoSchema); My

How to associate a mongoose model with the right MongoDB collection in Express.js app?

依然范特西╮ 提交于 2021-02-08 04:11:46
问题 how to point to particular collection in mongodb using mongoose ORM in express js app . NA var mongoose = require('mongoose'); var Schema = mongoose.Schema; var personSchema = new Schema({ name: String, age: Number }); module.exports = mongoose.model('person', personSchema); consider my mongo database have multiple collection ,so how will my above code will point to particular collection. 回答1: I'm really sorry my selected answer is not accurate. I can't delete it because it is accepted. The

How to associate a mongoose model with the right MongoDB collection in Express.js app?

梦想的初衷 提交于 2021-02-08 04:11:20
问题 how to point to particular collection in mongodb using mongoose ORM in express js app . NA var mongoose = require('mongoose'); var Schema = mongoose.Schema; var personSchema = new Schema({ name: String, age: Number }); module.exports = mongoose.model('person', personSchema); consider my mongo database have multiple collection ,so how will my above code will point to particular collection. 回答1: I'm really sorry my selected answer is not accurate. I can't delete it because it is accepted. The

Insert and return ID of sub-document in MongoDB document sub-document array

强颜欢笑 提交于 2021-02-07 20:18:42
问题 My node.js application will insert a sub-document into a nested sub-document array field of the following MongoDB document, and I need to determine the ID of the newly inserted sub-document: { "_id" : ObjectId("578d5a52cc13117022e09def"), "name" : "Grade 5 - Section A", "scores" : [{ "studentId" : ObjectId("5776bd36ffc8227405d364d2"), "performance" : [{ "_id" : ObjectId("57969b8fc164a21c20698261"), "subjectId" : ObjectId("577694ecbf6f3a781759c54a"), "score" : 86, "maximum" : 100, "grade" : "B

mongoose objectid to number

血红的双手。 提交于 2021-02-07 13:47:56
问题 How can I cast an ObjectId to a number? In my app I'm using the latest mongoose version and backbone on the clientside. My problem is that the ObjectId always ends up being put in between quotes which results in double quotes like ""233453452534"" in my jade templates and on my client. edit: I'm querying mongodb with this.users.find({},function(err,docs){ cb(null,docs) }) console.log(docs) shows { name: 'test', _id: 5220bc207f0c866f18000001, __v: 0 } in my template option(data-id=val._id) #

Tracking changes to fields using mongoose.js

本小妞迷上赌 提交于 2021-02-07 12:40:03
问题 I'm trying to figure out the best way to track changes to fields when using mongoose.js. For example, every time the name field on an object is set, I want to add a new entry to that object's history (as an embedded document) that looks something like { field: 'name', previous: 'foo', current: 'bar', date: '3/06/2012 9:06 am' } . I started by trying to use a plug-in that hooks .pre('save') but I can't figure out which fields have been modified without grabbing the old value from the database

Why does ISO date in Mongodb display one day earlier?

荒凉一梦 提交于 2021-02-07 10:32:23
问题 The stored date looks like this: ... "date_of_birth" : ISODate("1920-01-02T00:00:00Z"), ... Using moment, it is formatted in the model (in order to populate the input for updating the document) like this: AuthorSchema .virtual('date_of_birth_update_format') .get(function(){ // format in JavaScript date format (YYYY-MM-DD) to display in input type="date" return this.date_of_birth ? moment(this.date_of_birth).format('YYYY-MM-DD') : ''; }); Retrieved from the collection and displayed, it

findOneAndUpdate overwriting attributes in 2+ level deep object passed as doc

坚强是说给别人听的谎言 提交于 2021-02-07 08:33:43
问题 Let's say I have this schema: var UserSchema = new Schema({ name : { firstName : String, lastName : String } }); And I create this user: User.create({ name : { firstName : Bob, lastName : Marley } }, function (err, user) { if(err) { return console.error(err); } else { return console.log(user); } }); I noticed that if I do this: User.findOneAndUpdate({_id: userId}, { name: { firstName : "Damian" } }, function (err, user) { if(err) { return console.error(err); } else { return console.log(user);

findOneAndUpdate overwriting attributes in 2+ level deep object passed as doc

。_饼干妹妹 提交于 2021-02-07 08:32:52
问题 Let's say I have this schema: var UserSchema = new Schema({ name : { firstName : String, lastName : String } }); And I create this user: User.create({ name : { firstName : Bob, lastName : Marley } }, function (err, user) { if(err) { return console.error(err); } else { return console.log(user); } }); I noticed that if I do this: User.findOneAndUpdate({_id: userId}, { name: { firstName : "Damian" } }, function (err, user) { if(err) { return console.error(err); } else { return console.log(user);

Arrays of schema types on Mongoose

蹲街弑〆低调 提交于 2021-02-07 08:24:47
问题 I have a schema: var s = new Schema({ links: { type: [Url] } }); In this case I am using the url schema type from https://github.com/bnoguchi/mongoose-types - but I have tried this with other types. Mongoose doesn't seem to validate/use the schema type when in an array - works fine without the array. How can I define an array of schema types that will validate? 回答1: Answer from Mongoose creator: "Unless the Url is a subdocument, validation will not get triggered currently (there is a ticket