Tag: mongoose schema

如何在MongoDB中添加2个引用到单个模型?

我正在开发一个博客网站(如中等),其中将有一个用户模型,博客模型和类别模型。 基本思想是将用户存储在用户数据库中,博客数据库中的所有博客和类别数据库中包含可以发布的不同types的博客。 我想在BlogDB中存储用户(谁发布了博客)的参考和博客的类别。 我所做的模式如下 – 博客架构 – var blogSchema = new mongoose.Schema({ title: String, content: String, image: String, createdAt: Date, blogCategory : { id: { type : mongoose.Schema.Types.ObjectId, ref : "Category" } }, author: { id: { type: mongoose.Schema.Types.ObjectId, ref: "User" }, username: String }}); 用户架构 – var UserSchema = new mongoose.Schema({ firstname: String, lastname: […]

如何使一个领域取决于其他领域的价值meteorautoform?

我是meteor的新手,我想创build一个表单,其中一个字段的值决定了autoform中另一个字段的值 。 让我们把安装types设置为“A”,“B”和“C”,所以当我select“A”时,autoform将被加载。 我已经把这个表格作为通用的,即它将显示给所有A,B和C. {{#each users}} {{> afQuickField name='userEmail' value=userEmail readOnly=true }} {{> afQuickField name='Setup' value=type readOnly=true}} {{> afQuickField name='Profile' options='allowed' }} {{> afQuickField name='Purpose' }} {{> afQuickField name='count' options='allowed' }} {{> afQuickField name='PackageDirectory' options='allowed' }} {{> afQuickField name="logName" options=LogName }} {{/each}} 计数选项应该是: 1.对于“A”计数选项应该是9,11,12。 2.“B”是1。 3.“C”是5。 在架构中,我写了这样的代码 Setup:{ type: String, label:"Setup", optional:false, defaultValue:type }, count:{ […]

是否有可能使用mongoose直接查询子文档?

假设有一个用户模型和一个Post模型。 在这种情况下,用户会有很多post; 用户将是父母,邮政将是孩子。 是否有可能直接查询post? 例如,如果我想要做类似的事情 app.get('/post/search/:query', (req,res) => { Posts.find({title: req.params.query }, (err,post) => { res.send(JSON.stringify(post)) }) }) 或者必须做: app.get('/post/search/:query',(req,res) => { let resultsFromQuery = []; User.find({'post.title':req.params.query'}, (err,user) => { user.posts.forEach((post) => { if(post.title === req.params.query){ resultsFromQuery.push(post); } }) }) res.send(JSON.stringify(resultsFromQuery)) }) 编辑:这是我的模式。 用户模式(父) const mongoose = require('mongoose'), Schema = mongoose.Schema, PostSchema = require('./post.js'); let […]

Mongoose模式方法或原型函数?

我有一个关于函数和ES6类的问题。 我不确定从mongoose模式创build的新对象是否为每个新对象复制函数。 什么是最好的方法是什么时候使用ES6类。 我写了一个相关的例子来说明我的意思。 // models/User.js const mongoose = require('mongoose'); // User Schema const userSchema = new mongoose.Schema({ name: { type: String, required: true, unique: true, }, }); userSchema.methods.say = function testFunc(text){ console.log(`${this.name} said ${text}`) } module.exports = mongoose.model('User', userSchema); // models/Animals.js const mongoose = require('mongoose'); // Animal Schema class Animal extends mongoose.Schema { constructor() […]

TypeError:不能使用'in'运算符在一个中search'_id'

我正在尝试使用POST请求来检索提交的button的值,并在呈现新页面并传递值之前使用返回的值创build一个新的Mongoose模型条目。 当我尝试创build新的“types”,虽然我收到TypeError:“types错误:不能使用'在'运算符search'_id'在一个”。 有人可以帮我找出是什么驱动错误,以及如何解决它? 使用的forms: <form action="/round/type" method="POST" class="form-inline"> <div class="row"> <div class="col"> <button class="btn btn-lg btn-primary btn-block" name="roundType" value="one" type="submit">one</button> </div> <div class="col"> <button class="btn btn-lg btn-primary btn-block" name="roundType" value="two" type="submit">two</button> </div> <div class="col"> <button class="btn btn-lg btn-primary btn-block" name="roundType" value="three" type="submit">three</button> </div> <div class="col"> <button class="btn btn-lg btn-primary btn-block" name="roundType" value="four" type="submit">four</button> </div> </div> […]

使用mongoose更新深度嵌套的子文档

我试图阅读大部分类似的问题(而且有很多),但找不到任何我能理解的解决scheme。 如下所述的文件结构 – [ {RoomID:Room1, Boards:[ {BoardID:Board1, Devices:[ {DeviceID:Dev1} ….] } ….] } …..] 查询RoomID和BoardID后如何添加新设备?

带前钩的mongoose挽救计划不起作用

我是Nodejs和Mongoose的新手,事情一切正常,直到新版发布的承诺折旧的mongoose。 我看到它build议使用蓝鸟,我正在尝试做。 我有以下代码: app.post('/users', (req, res) => { var body = _.pick(req.body, ['email', 'password']); var user = new User(body); user.save().then(() => { return user.generateAuthToken(); }).then((token) => { res.header('x-auth', token).send(user); }).catch((e) => { res.status(400).send(e); }) }); 和前钩: UserSchema.pre('save', function (next) { var user = this; if (user.isModified('password')) { bcrypt.genSalt(10, (err, salt) => { bcrypt.hash(user.password, salt, (err, […]

如何通过使用Mongoose查询子文档来查找父文档?

在下面的示例中,如何查找特定成员标识具有Membership (子文档)的Account (父级)文档? 我曾尝试使用$elemMatch和直接查询对象。 注意:两个模式都有一个删除的标志字段( dd )。 如果设置了这个字段,logging已经被删除,不应该被返回: var mongoose = require('mongoose') var membershipSchema = new mongoose.Schema({ m : { type: mongoose.Schema.ObjectId, ref: 'member' }, // Member b : { type: Date, required: true }, // Begin Date e : { type: Date }, // End Date dd : { type: Date }, // Deleted […]

虚拟在Mongoose对象是未定义的?

我最近一直在学习MongoDB和Mongoose以学习MEAN栈。 使用这个课程: https : //www.edx.org/course/introduction-mongodb-using-mean-stack-mongodbx-m101x我试图为我的产品架构创build一个虚拟,显示价格更方便用户办法。 但是,打印出这个虚拟displayPrice它会出现未定义,除非通过toObject或toJSON访问,即使这样USD符号显示为一个问号。 我为任何愚蠢和明显的忽视道歉,我是这个数据库的新东西,无法find许多简单的解释事情的教程。 这里是代码: var mongoose = require("mongoose"); var Category = require("./categoryschema.js"); var productSchema = { name: { type: String, required: true }, // Pictures must start with http://" pictures: [{ type: String, match: /^http:\/\//i }], price: { amount: { type: Number, required: true }, currency: { type: String, enum: ["USD", […]

mongoose – 更新文档,添加新的密钥(有严格:false)

我有以下mongoose模型: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var companySchema = new mongoose.Schema({ name: String, long_name: String, address: String, telephone: String, mobile_phone: String, fax: String, email: String, url: String, config:{ language: String, account: String }, items:[{ name: String, internal_id: String, reference_code: String, description: String }] },{ timestamps: true, strict: false }); var Company = […]