mongoose-schema

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

How to store URL value in Mongoose Schema?

淺唱寂寞╮ 提交于 2021-02-18 11:45:08
问题 I am uploading the images from an IOS application to Firebase which returns to me the metadata including the URL of type URL . Should I store it of type String in the database like below code? or there is specific type for URL s? var schema = new Schema({ downloadURL: { type: String, createdDate: Date.now } }) 回答1: Well, As per the docs Monngoose doesn't have a schema type for URL. You could just use a string with RegExp to validate it or use some custome made type like this one var mongoose

mongoose populate in array of custom objects

主宰稳场 提交于 2021-02-11 14:42:28
问题 In the user model, I have an array of custom objects followedPlaylists which contains two attributes ( playlist: the id of the playlist, public: to determine whether it is public or not) as shown below const userSchema = new mongoose.Schema({ ..... other attributes followedPlaylists: [{ playlist: { type: mongoose.Schema.ObjectId, ref: 'Playlist', unique: true }, public: Boolean }] }) I want to populate on followedPlaylists.playlist so the response would be something like [{ playlist: * the

Mongoose: ref custom field name

99封情书 提交于 2021-02-10 17:52:37
问题 I am configuring Mongoose to work on an existing MongoDB, that has these two collections: Users - with fields: _id: ObjectId name: String org_id: ObjectId Organizations - with fields: _id: ObjectId name: String I want to be able to populate a User document by Organization data. So I've created these two Models: const userSchema = new Schema({ name: String, org_id: { type: Schema.Types.ObjectId, ref: 'Organization', }, }); const User = mongoose.model('User', userSchema); const

Mongoose's loadClass() with TypeScript

六月ゝ 毕业季﹏ 提交于 2021-02-09 10:59:31
问题 Mongoose accepts an ES6 class as the basis for a schema. The example from that link: class PersonClass { get fullName() { return `${this.firstName} ${this.lastName}`; // compiler error } } PersonSchema.loadClass(PersonClass); The schema's properties are not defined in the class, so the TypeScript compiler says: Property firstName does not exist on type PersonClass. A hack is to use a dummy constructor: constructor(readonly firstName: string, readonly lastName: string) { } However that is hack

Mongoose Compound Index Unique + Sparse

落花浮王杯 提交于 2021-02-08 08:16:44
问题 I want to create an index which ensures, that I don't have a duplicate serialNr within the combination of a manufacturer + art . But some items don't have a serialNr . These I don't want to check/index. Code mySchema.index({ serialNr: 1, art: 1 , manufacturer: 1, deleted: 1}, { unique: true, sparse: true) I tried it also with adding a partial Filter: partialFilterExpression: { serialNr: {$ne:null} } to the index options. Question How can I index it that inserting: [{art: 'a', manufacturer:'a'

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

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