Tag: 填充

Mongoose Populate Child ref返回null数组

正在检查Mongoose和相对较新的填充方法。 从孩子向父母填充时似乎工作完美,如下所示: var Comment = new Schema({ Message: { type: String } User: { type: ObjectId, ref: 'User' } }); var User = new Schema({ Username: { type: String } Comments: [{ type: ObjectId, ref: 'Comment' }] }); 以下按预期工作。 Comment.find({}).populate({ path: 'User' }).exec(function (err, comments) { if(err) handle(callback); // no error do something with comments/user. […]

在填充时连接一个string

这里是我的代码来填充专辑和他们的图像: server.get(url_prefix + '/image', function(req, res, next) { var album_id = req.query.album_id Album.findOne( {_id : album_id} ) .populate('images') .exec(function (err, album) { if (album) { return res.send({ status: { error: 0, message: "Successful" }, data: { album: album } }) } else return notify_error(res, "No Results", 1, 404) }) }) 相册架构: var AlbumSchema = new […]

Mongoose使用'path数组'来填充许多对象{path:objectpath,model:'Model'}

在我的networking应用程序中,订购(SingleOrder)产品后,客户应该检查优惠。 如果可用,那么我应该添加订单到ComboOfferOrder。 在那里,我想检查订单的付款状态。 另外,我必须得到整个产品清单。 我在我的数据库中的所有值在后端。 但是我无法为我的api方法填充“SingleOrder”中的任何对象。 我有下面的模式。 *User* { name : String, email : Sring, role : String } *BankTransaction* { type : String, transationReference: String, date : Date, amount : Number } *ComboOfferOrder* { customer :{ type :Schema.ObjectId, ref : 'User' }, order : { type :Schema.ObjectId, ref : 'SingleOrder' }, productList : [{ […]

mongoose – 如何限制填充()级别的深度?

我有两个模式: var categorySchema = mongoose.Schema({ title: String, blogs: [{ type: ObjectId, ref: 'Blog' }] }) var blogSchema = mongoose.Schema({ title: String, description: String, category: [{ type: ObjectId, ref: 'Category' }], created: {type: Number, default: new Date().getTime()} }) var Category = mongoose.model('Category', categorySchema) var Blog = mongoose.model('Blog', blogSchema) 它们是交叉引用的: Blog对象包含一个Category对象(ref)的数组,以便能够获取与该博客相关的所有类别。 Category对象包含一个Blog对象数组(refs),以便能够获得这个类别的所有博客。 问题是当我试图获得某个博客。 我需要填充类别数组来获得它的标题: Blog .findOne({_id: _._id}) […]

Mongoose填充返回null或undefined

对不起,如果这是一个问题,我已经search了Google&Stack几个小时,现在我得问了! 我有两个模式,用户和故事,如下所示。 我想引用用户的故事使用Ref选项填充查询 – 我以前使用mySQL,所以想要尝试复制JOIN语句。 每当我尝试使用填充,我只是得到objectID返回,或null(如下所示)。 编辑11月12日修复硬编码的ID和添加控制台数据 故事schema.js var mongoose = require('mongoose'), Schema = mongoose.Schema, User = require('./user-schema'); var StorySchema = new Schema({ title: { type: String, required: true }, author_id: { type: Schema.Types.ObjectId, ref: 'User' }, summary: { type: String, required: true }, rating: { type: String, required: true } }); module.exports = mongoose.model('Story', […]

mongoose“填充”意味着什么

所以一般是什么“ 填充 ”? 指的是对数据库的一些操作。 我以前听说过,但从来没有听错

保存后,Mongoose返回填充数组

我想返回一个更新的对象作为JSON,其中更新是设置一个objectIDs数组。 我希望返回的反对将该数组填充。 例如,我有以下(简化)模型: var UserSchema = new mongoose.Schema({ username: {type: String, unique: true, required: true}, friends: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}] }); 在我的控制器中,我有: exports.saveFriends = function(req, res) { User.findById(req.params.user_id, function(err, user) { // req.body.friends is JSON list of objectIDs for other users user.friends = req.body.friends user.save(function(err) { user.populate({path: 'friends'}, function(err, ticket) { if (err) { res.send(err); […]

在Mongoose中填充自定义索引

是否有可能使用不同的索引(而不是_id)在MongoDB中填充字段。 这个索引是唯一的,由_id填充在我的问题的背景下根本没有用处。 我正在谈论这样的事情: pop_options: { path: 'cars', model: 'Car', index: 'Car.regnumber' } User.find({name: 'John'}).populate(pop_options).exec(function(err, user){ //Code }) 非常感谢!

NodeJS + Mongoose:人口不起作用

我想添加到Blog对象的几个Categories对象,并在这之后获得访问博客的第一类。 var ObjectId = mongoose.Schema.Types.ObjectId; var categorySchema = mongoose.Schema({ title: String, blogs: [{ type: ObjectId, ref: 'Blog' }] }) var blogSchema = mongoose.Schema({ title: String, description: String, category: [{ type: ObjectId, ref: 'Category' }], created: {type: Number, default: new Date().getTime()} }) var Category = mongoose.model('Category', categorySchema) var Blog = mongoose.model('Blog', blogSchema) exports.addBlog = function (object, […]

如何发布和更新模式数组中的模式并打印它?

你好,我是新的节点js和mongoose我有两个计划,我想在其他架构模式对象我的计划 user.js的 // grab the things we need var mongoose = require('mongoose'); var Schema = mongoose.Schema; var Genre = require('../Models/genre'); // create a schema var userSchema = new Schema({ name: { type: String, required: true, unique: true , index:true}, email: { type: String, required: true, unique: true }, password: { type: String, required: true }, […]