Tag: mongoose schema

将网站configuration存储为Mongoose模型

我有网站configuration(目前存储为JSON文件),我想将其移动到MongoDB,可能使用Mongoose来处理读写操作,并通过架构进行validation。 configuration是一个有限的键的对象,类似于: { siteOffline: false, storeOffline: false, priceMultipliers: { a1: 0.96 a2: 0.85 }, … } 是否应该使用键值条目进行收集? 在这种情况下不知道如何执行Mongoose模式。 是否应该使用单个文件进行收集? 不确定如何保证一次只有一个文档。 任何其他选项?

Mongoose:schema._getVirtual不是一个函数

我突然遇到麻烦(在我更新Mongoose包版本之前工作正常)。 目前使用Mongoose 4.7.6。 var userSchema = require('schemas/user'), User = db.model('User', userSchema); // db is already known User .findById(user._id) // user is already known due to auth .populate('group currentPlayer') .exec(function (findErr, userPlayer) { … }); 如果我的用户架构是必要的,我会发布,但目前还没有由于其长度(虚拟,方法,静态)。 错误: /app/node_modules/mongoose/lib/model.js:2986 var virtual = modelForCurrentDoc.schema._getVirtual(options.path); ^ TypeError: modelForCurrentDoc.schema._getVirtual is not a function at getModelsMapForPopulate (/app/node_modules/mongoose/lib/model.js:2986:49) at populate (/app/node_modules/mongoose/lib/model.js:2660:15) at […]

如何解决不能读取nodejs应用程序中未定义的属性“推”?

当我将值添加到子文档时,在命令提示符中显示错误 像不能读取属性push 。 我该如何解决这个问题? 这是my schema code与这些帮助我赋予父架构的价值观 但我不能给这个子文档的值: var venueSchema = new Schema({ name: { type: String, required: true, unique:true }, address: { type: String, required: true } }, { timestamps: true }); // create a schema var batchSchema = new Schema({ batchname: { type: String, required: true, unique: true }, activityname: { type: String, […]

Mongoose返回空而mongodbshell中的相同查询正常工作

我知道这个问题在这里已经有很多次的问题了,我也经历了几个类似的问题,但是没有一个似乎对我有帮助。 我有两个名为users集合,他们的posts和模型如下所示: 用户 var mongoose = require('mongoose').set('debug', true); var Schema = mongoose.Schema; var usersSchema = new Schema({ name: {type: String, required: true} }); var User = mongoose.model('user', usersSchema, 'users'); module.exports = User; post var mongoose = require('mongoose').set('debug', true); var Schema = mongoose.Schema; var postsSchema = new Schema({ content: String, user: { type: Schema.ObjectId, ref: 'users', […]

Nodejs Mongoose获取属性值的结果数组

我怎样才能得到一个没有对象结构的属性值的数组。 这是我的模式 _id: { type: Schema.Types.ObjectId, ref: "User" }, services: [{ _id:false, service_category: { type: Schema.Types.ObjectId, ref: "ServiceCategory" }, sub_services :[{ _id:false, service : { type: Schema.Types.ObjectId, ref: "Service" } }] }] 这就是我要查询结果的方式 Vendor.find({ '_id': req.user._id, 'services.service_category':req.body.category_id},'services.sub_services.service').exec(function (err, rtnobj) { if (err) { console.log(err); return (err); } else{ res.send(rtnobj); } }) 但它给了我这个输出 [ { "_id": […]

我应该在哪里抓住这个Promise MongoError?

我升级了我的mongoose,所以当然我开始得到这些: DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html 所以我加了mongoose.Promise = global.Promise 。 都好。 除了…现在我得到这个人: (node:20760) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: exception: Index with name: stampRoundedToNearestHour_1 already exists with different options (node:20760) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise […]

根据Mongoose模式validation对象而不保存为新文档

我试图validation一些将被插入到一个新文档中的数据,但不是在很多其他事情需要发生之前。 所以我打算给静态方法添加一个函数,希望能够在模型模式中validation数组中的对象。 下面是代码: module.exports = Mongoose => { const Schema = Mongoose.Schema const peopleSchema = new Schema({ name: { type: Schema.Types.String, required: true, minlength: 3, maxlength: 25 }, age: Schema.Types.Number }) /** * Validate the settings of an array of people * * @param {array} people Array of people (objects) * @return {boolean} */ peopleSchema.statics.validatePeople […]

如何使用mongoose和nodejs删除模型中的项目id

这是我的模式 var purchaseOrderModel = function () { var itemSchema = mongoose.Schema({ strBarCode: {type: String }, strItemName: {type: String }, strQuantity: {type: String } }); var purchaseOrderSchema = mongoose.Schema({ strSupplierID: {type: String, required: true }, strInvoiceNo: {type: String,required: true }, dteInvoiceDate: {type: Date, required: true }, items: [itemSchema], created_by: { type: mongoose.Schema.Types.ObjectId, ref: 'user' } }); […]

如何更新mongoose中的引用数组

我有一个Group Collection,它有一个Reference的成员数组。 两个对象如下所示相互连接。 当我向该组添加新成员时,Group对象的成员字段需要更新。 我怎么能用mongoose update操作符来做到这一点。 var MemberSchema = new Schema({ name:{ type:String, default:null }, user_id:{ type : Schema.ObjectId, ref : 'User', default : null } }); var GroupSchema = new Schema({ name:{ type:String, default:null }, description:{ type:String, default:null }, members:[MemberSchema], },{collection:"groups"}); 先谢谢你。 更新 我添加了一个组的样本文件。 { "_id" : ObjectId("586a2e694467c41218b302c3"), "members" : [ { "_id" : […]

用Mongoose计算Mongodb中的多个集合的大小

我需要计算两个集合(设备,房间)内的文档数量。 我将Devices Schema和Rooms Schema中的细节保存为单独的集合。 如何查询这两个集合并返回文档数量?