Tag: 模式

聚合中的mongoose模式方法

怎样才能在聚合中访问方法? let mongoose = require("mongoose"), Schema = mongoose.Schema; let UserSchema = new Schema({ name: String, lastname: String }); UserSchema.methods.fullname = () => { return this.name + " " + this.lastname; }; let UserModel = mongoose.model("user",UserSchema); let query = [ { $match: { "name": /foo/i } } ]; UserModel.aggregate(query, (err, users) => { if(err) throw new […]

mongoose,如何在子文档中loggingupdate_time

我有一个mongoose模式是: 这是我的mongoose模式 'use strict'; module.exports = app => { const mongoose = app.mongoose; const custSchema = new mongoose.Schema({ belong: { // 所属信息 user_id: { type: 'String', required: true }, type: { type: 'Number', required: true }, // bee、4s店、平台 username: { type: 'String', required: true }, // 用户名 }, delete: { type: Number, default: 0 }, […]

是否有可能为Mongoose SchemaTypes禁用自动types转换?

对于具有此架构的模型… { name: { type: String } } …以下内容自动将提供的值转换为string,而不是强制执行types: document.name = 2; document.validate(err => { // Err is null, document.name === '2' }) 有没有简单的方法来禁用这种行为?

将数组与包含对象的数组进行比较

这是我的集合架构: var objectSchema = new Schema({ members: [{ user_id: ObjectId, settings: { type: Boolean } }], title: String }); 现在我试图search具有特定成员的对象(由其“user_id”标识,例如[“asdf123lkd”,“asdf1223”])。 有什么方法可以search这些对象吗? 谢谢!

MongoDB:多个填充子文档不被返回

我正在尝试返回文档和子文档的完整数据对象。 它返回的是文档和子文档的ObjectID。 我已经尝试了多个填充语句,这也是行不通的。 我错过了什么? 用户架构 module.exports = function (connection) { var mongoose = require('mongoose'); var Schema = mongoose.Schema; var user_fb = new mongoose.Schema({ name: String, location: String, fb_id: Number, fb_image: String }); return connection.model('User_Fb', user_fb);; } Feed架构 module.exports = function (connection) { var mongoose = require('mongoose'); var Schema = mongoose.Schema; var locationSchema = new Schema({ […]

Mongoose无法读取具有复杂模式的未定义属性

我有以下用户架构: var mongoose = require('mongoose'); var userSchema = mongoose.Schema({ local: { email: String, password: String }, education: { school: { start: Boolean, name: String, graduationYear: String }, college: { start: Boolean, name: String, career: String, enterYear: String, graduationYear: String } } }); 现在,当我提交包含所有这些信息的表单时,我的应用程序将POST请求路由到以下函数: exports.postEditEducation = function (req, res){ process.nextTick(function (){ var user = req.user; user.education.school.start […]

Mongoose中的dynamic模式密钥

我有一个简单的要求,以获得dynamic密钥和他们的价值插入mongo。 像这样的东西: [ {"key1": "val1"}, {"key2": "val2"} ] 为此,我已经创build了模式为:当我读到[Schema.Types.Mixed] ,但它只是使数据types的分配值dynamic,而不是我的情况下的关键。 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var myschema = new Schema({ key: [Schema.Types.Mixed] }); module.exports = mongoose.model('DataCollection', myschema); 任何人都可以指出,我错过了。 这是我的输出,显示空白值。 提前致谢。

mongodb mongoose nodejs express4为什么自动在Object Array字段中插入一个_id?

这可能是关于mongodb中_id概念性问题。 我知道mongodb会自动插入一个_id字段,如果你没有在文档中设置关键字段。在我的情况下,我定义了一个字段为对象数组,我不知道为什么它总是在这个数组中的每个对象中创build一个_id领域。 我很感激,如果有人能为我澄清。 mongoose模型scheme定义: module.exports = mongoose.model("Application", { Name: String, Description: String, Dependency: [ { App_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Application' }, Priority: Number } ] }); 这是一个更新操作,请求数据是: { _id: '571953e33f33c919d03381b5', Name: 'A Test Utility (cmd)', Description: 'A Test Utility (cmd)' Dependency: [ { App_id: '571953e33f33c919d03381b6', Priority: true }, { App_id: '571953e33f33c919d03383da', Priority: 0 } […]

mongoosevalidation器

我有2个模式,其中包含validation器,PhotosSchema,UserSchema。 因为mongoose'更新'function使用本地mongo驱动程序(据我所知),它不会触发和validation检查。 因此在两个模式中我都这样做: PhotosSchema.pre('update', function(next) { this.options.runValidators = true; next(); }) 在照片模式中,它效果很好! 它在每次更新之前都会运行validation。 但在用户,由于某种原因,它不会触发validation,我清楚地看到所有的validation,当我检查'这'在'更新'前,但我真的不明白什么是它不会甚至不会调用该函数。 文件附在这里:)照片模式: 'use strict'; var mongoose = require('mongoose'), crate = require('mongoose-crate'), S3 = require('mongoose-crate-s3'); var path = require('path'); var consts = require('../../config/environment/shared') var PhotosSchema = new mongoose.Schema({ userId :{ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, photoIndex: { type: Number, validate: [photosLimit, […]

如何创buildtypes为string或null的JSON模式属性

我的模式看起来像这样: schema: { type: 'object', properties: { id: { type: 'string' } } } 我想被允许设置string或null,我该怎么做?