Tag: mongoose populate

Mongoose:如何填充嵌套数组(深层)

我在填充深层时遇到了麻烦。 我search并阅读了很多页面,但无法parsing。 这是我的模式: var boxSchema = new mongoose.Schema({ name: { type: String, unique: true }//, required: true , height: { type: Number },//, required: true width: { type: Number }, sensors: [ { type: mongoose.Schema.ObjectId, ref: 'Sensor', } ] }); var slotSchema = new mongoose.Schema({ slotPosition: { type: Number, unique: true }, startDate: { type: […]

反向填充mongoose

我想弄清楚如何使用我的订单集合中的businessId属性填充我的业务订单。 我试过,但不能得到它的工作。 https://www.npmjs.com/package/mongoose-reverse-populate 任何想法,我做错了或如何完成我需要做的其他build议? Business.findById(id).exec(function(err, business) { var opts = { modelArray: business, storeWhere: "orders", arrayPop: true, mongooseModel: Order, idField: "businessId" }; reversePopulate(opts, function(err, businessAndOrders) { req.business = businessAndOrders; next(); }); }); – var BusinessSchema = new Schema({ businessName:{ type:String } /*…….*/ }); var OrderSchema = new Schema({ data: { type: String, } created: { […]

如何填充子`ref`ed文件mongoose数组?

我正在使用Mongoose 4.4.0版的MEAN堆栈应用程序,并且在填充子文档数组时遇到了问题。 ( 在SO的其他地方找不到解决办法 )。 以下说明模型的结构: 签证模式 var visaSchema = new mongoose.Schema({ from: {type: Schema.Types.ObjectId, ref: 'Country'}, to: {type: Schema.Types.ObjectId, ref: 'Country'}, requirements: [ {type: Schema.Types.ObjectId, ref: 'Requirement'} ] }); exports.model = mongoose.model('Visa', visaSchema); 需求模型 var requirementSchema = new mongoose.Schema({ visa: {type: Schema.Types.ObjectId, ref: "Visa"}, title: String, body: String }); exports.model = mongoose.model('Requirement', requirementSchema); 以上基本上构成了Visa和Requirement之间的一对多关系。 […]

Keystone js – populateRelated字段显示为未定义

每个应用程序与用户有1对1的关系。 每个旅程都有n个应用程序 下面是我的以下代码,用于在所有旅程的应用程序中填充所有用户(学生)数据: view.on('init', function(next) { Trip.model.find() .exec(function(err, trips){ keystone.populateRelated(trips, 'students[user]', function(err) { locals.trips = trips; next(); }); }); }); 这是Trips与应用程序(StudentApp)的关系: Trip.relationship({ ref: 'StudentApp', refPath: 'finalTrip', path: 'students' }); 现在在前端,我可以打印一个用户的string化的JSON就好了: each trip in trips each app in trip.students p=app.user 然而,当我尝试访问一个字段,例如app.user._id我得到以下错误: Cannot read property '_id' of undefined 我感觉这是一个asynchronous的问题,但我不能为了我的生活找出如何解决这个问题。 帮助将不胜感激。 谢谢。

如何正确使用函数populate()

我使用函数.populate()来获取按category分组的equipements ,所以我的模型是这样的 var mongoose = require('../config/db'); var EquipementSchema = mongoose.Schema({ libelle: String, marque: String, category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' } }); module.exports = mongoose.model('Equipement', EquipementSchema); 路线: router.get('/categorie_id', function(req, res, next){ models.equipement.aggregate([ { $group : { _id : '$categorie_id', equipements: { $push: '$$ROOT' } } } ].exec(function(err , results){ if(err) res.json({error: err}); res.json(results); })); }); […]

使用cuid来replacemongoose ObjectIds

我正在考虑使用cuid( https://www.npmjs.com/package/cuid )来replacemongoose的自然生成的objectids,这样我的模型中的_id就会变成cuid而不是生成的。 问题: 你怎么做到这一点 有没有什么副作用 这将如何影响人口? 1是如此简单: import mongoose from 'mongoose'; import cuid from 'cuid'; const Schema = mongoose.Schema; export const departmentSchema = new Schema({ _id: { type: 'String', default: cuid(), required: true }, name: { type: 'String', required: true }, sector: { type: 'String', required: true }, }); ?? 谢谢

mongoose和节点JS – 提高发现

我有集合A和集合B.我需要获得所有B(不同的是最好)在A有一个简单elemMatch。 在这种情况下,A是形成,B是过程。 我只需要正在提取的字段,没有别的。 目前我有这个工作: Formation.find({}, { _id: 0, course: 1 }).elemMatch('trainees', { 'trainee': traineeID }).populate('course', '_id name').exec(function(err, formations) { if(err) return ; console.log(formations); // to fix the array format var courses = formations.map(function(f){ return f.course }); console.log(courses); // to delete repeated values courses = courses.filter((item, index, self) => self.findIndex((i) => { return i._id === item._id; […]

Mongoose填充API请求和Postman,返回空数组?

我创build了一个带有nodejs,mongodb / mongoose和express的api,以使CRUD请求与json数据交互,更具体地说,使用两个mongodb集合(1:datas 2:produits)。 “数据”集合绑定到“用户”模式。 “产品”集合绑定到“产品”模式。 我的问题是: 在“User”模式中,我有一个Productlist ,它将从产品集合中获取/保存产品的ID。 我的用户模式如下所示: //Get module var mongoose = require('mongoose'); var prodSch = require('../models/productSchema'); var Schema = mongoose.Schema; //Create user shema var user = new Schema({ "UIDUser": String, "IDUser": String, "UIDACL": String, "DatasUser": { "acl_user_id": String, "prenom": String, "role": String, "nom": String, "pseudo": String, "email": String }, "Productlist" : […]

mongoose:适当地devise一个关系模式

我试图在我的mongodb / monogoose数据库中的几个对象之间build立一些关系数据,使用引用(在属性中存储ID)。我写了我已经实现的和我的缺点。 这是一个广阔的职位,请阅读我的TL; DR之前阅读所有内容。 一些代码的高级答案也是可以接受的。 我在我的Mongoose数据库中有以下模式: 用户: UserSchema: { _id: (MongoID) isActive: firstName: lastName: email: phone: role: hash: address: } 车辆: VehicleSchema: { _id: (MongoID) internal_id: (linking id) metadata: (product name, product description) } 值: ValueSchema: { _id: (MongoID) internal_id: (linking id) todayPrice: xxx.xx history [ {…}, {…}, {…}] } 每个user都有一个garage ,他们可以存放vehiclestypesVehicleSchema 。 vehicle关系需要以下function: […]

mongoose多深填充

说,我有Manager架构: { name: { type: String }, clients: [{ type: Mongoose.Schema.ObjectId, ref: 'Client'}] } 而且,我有Client模式: { name : { type String }, cars : [{ type: Mongoose.Schema.ObjectId, ref: 'Car' }], houses: [{ type: Mongoose.Schema.ObjectId, ref: 'House' }] } 还有Car和House (他们的结构对于这个问题并不重要,我怎样在单个.populate()调用中深入填充多个Client的字段? 我试过了什么: Manager.find() .populate({ path: 'users', populate: { path: 'cars' }, populate: { path: 'houses' } […]