Tag: mongoose schema

虚拟领域的mongoose别名

我想在mongoose的虚拟领域做一个别名。 这是定义的模式: let userSchema = new mongoose.Schema({ firstName: String, lastName: String }); 我还定义了一个名为fullName的虚拟字段,它组合了firstName和lastName的值: userSchema.virtual('fullName').get(function () { return this.firstName + ' ' + this.lastName; }); 这个问题,是否有可能有一个别名(如name )这个虚拟领域, 而不必创build另一个虚拟领域与单独的setter和getter函数?

Mongooseasynchronous调用内循环与asynchronous库

我刚开始使用nodejs / mongoose,我想我有一个经典的asynchronous问题。 有人可以指导我如何解决这个asynchronous问题? 我有这个函数“getAreasRoot”和里面我有一个循环来填充另一个asynchronous函数的结果的儿童。 我怎样才能解决它与asynchronous库? areaSchema.statics.getAreasRoot = function(cb: any) { let self = this; return self.model("Area").find({ parentId: null }, function(err: any, docs: any){ docs.forEach(function(doc: any){ doc.name = "Hi " + doc.name; doc.children = self.model("Area").getAreasChildren(doc._id, function(err: any, docs: any){}); }) cb(err, docs); }); }; areaSchema.statics.getAreasChildren = function(id: any, cb: any) { return this.model("Area").find({ parentId: null […]

Mongoose – ObjectID作为关键?

我想在我的mongoose模型中有一个对象(例如“成分”),其中的键是ObjectIDs,它们的值是数字。 有可能吗? 我应该如何定义我的mongoose模式? 你可以在下面find一个例子。 示例JSON: { "_id": ""5a2539b41c574006c46f1a07", "name": "xyz", "ingredients": { "5a23f5e6159f5c3438c75971": 50, "5a23f60b159f5c3438c75972": 50, "5a255b04c9d9c40ac8927dd5": 50 } } 提前谢谢你的帮助。

将项目添加到混合模式数组时,mongoose.save的结果不正确

以下是显示问题的function: var mongoose = require('mongoose'); var connection = mongoose.createConnection('mongodb://localhost:27017'); connection.once('open', function () { var schema = new mongoose.Schema({ obj: [{}] //mongoose.Schema.Types.Mixed }); var Model = connection.model('mtest', schema); var model = new Model({ obj: [{ name: 'Original' }] }); model.save(function (err, res) { console.log('result 1', res); Model.findOne({_id: res._id}, function (err, res) { res.obj[0].name = 'Modified'; res.obj.push({ […]

Mongoose,在模式(枚举)中定义OR引用?

thread = new mongoose.Schema({ post: {type: Schema.Types.ObjectId, ref: 'Post'}, comment: {type: Schema.Types.ObjectId, ref: 'Comment'}, }) 但实际上,线程只能有post或评论,不能同时拥有。 所以理想的定义应该像枚举 thread = new mongoose.Schema({ data: {type: Schema.Types.ObjectId, ref: 'Post'} OR?? {type: Schema.Types.ObjectId, ref: 'Comment'}, }) 我如何在Mongoose中定义这种types的东西? 或者做这种事情的正确方法是什么?

填充多个级别不起作用

First = new mongoose.Schema({ name: String, second: {type: Schema.Types.ObjectId, ref: 'Second'}, }); Second = new mongoose.Schema({ name: String, third: {type: Schema.Types.ObjectId, ref: 'Third'}, }); Third = new mongoose.Schema({ name: String }); First.find({}).populate({ path: 'Second', populate: { path: 'Third'} }).exec(function(err, result) { console.log(result) }) 首先填充是好的,但第三总是null 。 意思是我有这样的一些东西: { name: 1, second: { name: 2, third: null […]

将mongoose模型包含在node.js文件中包含的单个js中?

我正在创build节点应用程序使用mongo mongoose。我有模型单独的文件,其中所有数据模块创build,我想configuration所有数据模型到一个js文件,我们必须包括在node.js文件(server.js)在这里,我有user.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; module.exports = function() { var users = new Schema({ name : String , body : String , date : Date }); mongoose.model("users", users); }; books.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; module.exports = function() { var books = new Schema({ title : String […]

Mongoose文档创build的最佳实践,其中字段中的项目数量未知

我查了一下左右,写了一些演示代码,写了一些testing来实现学校pipe理系统。 我想从更多用于mongoose的开发者那里了解到的是,如何创build这个模式的最佳实践是,如何从这个文档中添加我想要的地址和联系费用。 我做出了自己的解决scheme,但是我不知道是不是最优雅可行的方法,我想要经验丰富的人的意见。 我应该为地址,电子邮件和电话号码创build单独的模型吗? 我创build了这个模式。 它仍然有一些伪代码,但为了给出一般的想法是好的。 var student = { name: String, surname: String, currentClass: { type: mongoose.Schema.Types.ObjectId, ref: "Classes" }, birthday: { year: Number, month: Number, day: Number }, address: [{ name: String, zip: Number, address: String, city: String, state: String, complement: String }] accountable: { name: String, surname: String, email: [{ type: String, […]

如何查找Mongoose中的对象数组?

我有这样的Mongoose.Schema: const pixelSchema = mongoose.Schema({ x: String, y: String, color: String, }); 另外我有这样的对象数组: let pixels = [ {x: 0, 1: 0, color: 'blue'}, {x: 0, y: 1, color: 'blue'}, {x: 0, y: 2, color: 'blue'}, ] 我怎样才能检查这个元素之一是否已经存在于数据库中? 我的解决scheme现在看起来像这样,但我认为这是非常低效的。 pixels.map(pixel => { Pixel.find(pixel, (err, pixels) => { if (pixels) { console.log('Find!'); } }); });

索引在mongoose文件

我是一个searchfunction的工作,我必须在文档中的多个字段进行search。 我有以下情况: 1)获取field1,field2,field3,field4与string(nayyar)匹配的所有文档 2)获取field1,field2,field3,field4与string(nayyar)匹配的所有文档,性别应该是(input值)或“N / A”。 3)获取field1,field2,field3,field4与string(nayyar)匹配的所有文档,age应该在ageHigh和ageLow之间,AND性别应该是(input值)或'N / A'。 4)获取field1,field2,field3,field4与string(nayyar)匹配的所有文档,并且age应该在ageHigh和ageLow之间,AND性别应该是(input值)或'N / A'AND ArrayField'atoz'。 var Schema = new Schema({ lowAge : { type: Number, default: 0 }, highAge : { type: Number, default: 0 }, gender : { type: String, default: 'N/A' }, field1 : { type: String }, field2 : { type: String }, field3 […]