mongoose

Mongoosejs refresh a document

十年热恋 提交于 2021-02-19 01:20:51
问题 Suppose I have a document for example: var doc = Model.findOne({name:"name"}); Now if the document gets edited trough another connection the the database, doc doesn't hold the right information. I do need it, so I have to "refresh" or "redownload" it from the database. Is there any way to do this with only the object "doc"? 回答1: Assuming doc contains the document instance to refresh, you can do this to generically refresh it: doc.model(doc.constructor.modelName).findOne({_id: doc._id},

MongoDB/Mongoose index make query faster or slow it down?

梦想与她 提交于 2021-02-19 01:06:28
问题 I have an article model like this: var ArticleSchema = new Schema({ type: String ,title: String ,content: String ,hashtags: [String] ,comments: [{ type: Schema.ObjectId ,ref: 'Comment' }] ,replies: [{ type: Schema.ObjectId ,ref: 'Reply' }] , status: String ,statusMeta: { createdBy: { type: Schema.ObjectId ,ref: 'User' } ,createdDate: Date , updatedBy: { type: Schema.ObjectId ,ref: 'User' } ,updatedDate: Date ,deletedBy: { type: Schema.ObjectId, ref: 'User' } ,deletedDate: Date ,undeletedBy: {

MongoDB/Mongoose index make query faster or slow it down?

妖精的绣舞 提交于 2021-02-19 01:06:16
问题 I have an article model like this: var ArticleSchema = new Schema({ type: String ,title: String ,content: String ,hashtags: [String] ,comments: [{ type: Schema.ObjectId ,ref: 'Comment' }] ,replies: [{ type: Schema.ObjectId ,ref: 'Reply' }] , status: String ,statusMeta: { createdBy: { type: Schema.ObjectId ,ref: 'User' } ,createdDate: Date , updatedBy: { type: Schema.ObjectId ,ref: 'User' } ,updatedDate: Date ,deletedBy: { type: Schema.ObjectId, ref: 'User' } ,deletedDate: Date ,undeletedBy: {

MongoDB/Mongoose index make query faster or slow it down?

天大地大妈咪最大 提交于 2021-02-19 01:05:46
问题 I have an article model like this: var ArticleSchema = new Schema({ type: String ,title: String ,content: String ,hashtags: [String] ,comments: [{ type: Schema.ObjectId ,ref: 'Comment' }] ,replies: [{ type: Schema.ObjectId ,ref: 'Reply' }] , status: String ,statusMeta: { createdBy: { type: Schema.ObjectId ,ref: 'User' } ,createdDate: Date , updatedBy: { type: Schema.ObjectId ,ref: 'User' } ,updatedDate: Date ,deletedBy: { type: Schema.ObjectId, ref: 'User' } ,deletedDate: Date ,undeletedBy: {

how to update nested object of mongoose document for only provided keys

佐手、 提交于 2021-02-18 22:41:32
问题 I am going to update some fields of mongoose document according to provided keys. For example, When we present mongoose document in json. user: { address: { city: "city" country: "country" } } And update params is given like this. address: { city: "city_new" } when I run the mongoose api like this. let params = { address: { city: "city_new" } } User.set(param) It replace whole address object and final result is user: { address: { city: "city_new" } } it just replace address field, but I want

how to update nested object of mongoose document for only provided keys

十年热恋 提交于 2021-02-18 22:41:08
问题 I am going to update some fields of mongoose document according to provided keys. For example, When we present mongoose document in json. user: { address: { city: "city" country: "country" } } And update params is given like this. address: { city: "city_new" } when I run the mongoose api like this. let params = { address: { city: "city_new" } } User.set(param) It replace whole address object and final result is user: { address: { city: "city_new" } } it just replace address field, but I want

Clean up dead references with Mongoose populate()

别说谁变了你拦得住时间么 提交于 2021-02-18 18:59:49
问题 If a user has an array called "tags": var User = new Schema({ email: { type: String, unique: true, required: true }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref:'Tag', required: true }], created: { type: Date, default: Date.now } }); and I do a populate('tags') on a query: User.findById(req.params.id) .populate("tags") .exec(function(err, user) { ... }); If one of the tags in the list has actually been deleted, is there a way to remove this dead reference in "tags"? Currently, the

Clean up dead references with Mongoose populate()

≡放荡痞女 提交于 2021-02-18 18:59:32
问题 If a user has an array called "tags": var User = new Schema({ email: { type: String, unique: true, required: true }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref:'Tag', required: true }], created: { type: Date, default: Date.now } }); and I do a populate('tags') on a query: User.findById(req.params.id) .populate("tags") .exec(function(err, user) { ... }); If one of the tags in the list has actually been deleted, is there a way to remove this dead reference in "tags"? Currently, the

Mongoose VersionError: No matching document found for id when document is being saved

非 Y 不嫁゛ 提交于 2021-02-18 13:01:18
问题 I am repeatedly seeing the following error when synchronizing a users cart through a "/sync/" API request. This is called whenever a user changes the contents of their shopping cart. VersionError: No matching document found for id "2y4b1hq601cd013e0af25e32" version 4 modifiedPaths "cart, cart.items, cart.updatedAt" at VersionError.MongooseError [as constructor] (/node_modules/mongoose/lib/error/mongooseError.js:13:11) at new VersionError (/node_modules/mongoose/lib/error/version.js:18:17) at

Is it possible to create a multi-select enum in mongoose

元气小坏坏 提交于 2021-02-18 12:32:07
问题 I have a model with an enum field, and currently documents can have any single value from the enum. I want documents to be allowed to have an array of values, but have mongoose enforce that all of the values are valid options that exist in the enum - is that possible? Essentially I want the equivalent of a HTML <select multiple> element instead of a <select> 回答1: Yes, you can apply an enum to a path that's defined as an array of strings. Each value will be passed to the enum validator and