假设有以下3种型号:
var CarSchema = new Schema({
name: {type: String},
partIds: [{type: Schema.Types.ObjectId, ref: 'Part'}],
});
var PartSchema = new Schema({
name: {type: String},
otherIds: [{type: Schema.Types.ObjectId, ref: 'Other'}],
});
var OtherSchema = new Schema({
name: {type: String}
});
Run Code Online (Sandbox Code Playgroud)
当我查询汽车时,我可以填充零件:
Car.find().populate('partIds').exec(function(err, cars) {
// list of cars with partIds populated
});
Run Code Online (Sandbox Code Playgroud)
有没有办法在mongoose中填充所有汽车的嵌套零件对象中的otherIds.
Car.find().populate('partIds').exec(function(err, cars) {
// list of cars with partIds populated
// Try an populate nested
Part.populate(cars, {path: 'partIds.otherIds'}, function(err, cars) {
// This does not populate all the otherIds …Run Code Online (Sandbox Code Playgroud) 您是否可以在mongoose模式中使用对几个不同模式选项的引用来填充数组?
为了澄清这个问题,请说我有以下模式:
var scenarioSchema = Schema({
_id : Number,
name : String,
guns : []
});
var ak47 = Schema({
_id : Number
//Bunch of AK specific parameters
});
var m16 = Schema({
_id : Number
//Bunch of M16 specific parameters
});
Run Code Online (Sandbox Code Playgroud)
我可以用一堆ak47 或 m16 填充枪阵吗?我可以把BOTH放在同一个枪阵中吗?或者它是否需要在资产数组中使用填充ref,这样会将其限制为单个特定类型?
guns: [{ type: Schema.Types.ObjectId, ref: 'm16' }]
Run Code Online (Sandbox Code Playgroud)
我知道我可以为不同的枪类型提供单独的数组,但是随着项目的扩展,这将在模式中创建大量的额外字段,其中大部分将根据加载的方案保留为空.
var scenarioSchema = Schema({
_id : Number,
name : String,
ak47s : [{ type: Schema.Types.ObjectId, ref: 'ak47' }],
m16s: [{ type: Schema.Types.ObjectId, ref: 'm16' …Run Code Online (Sandbox Code Playgroud) 我尝试使用npm mongoose-paginate但是填充不能正常工作
这是我的 UsersSchema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var usersSchema = new Schema({
name : String,
created_at : { type : Date, default : Date.now }
});
module.exports = mongoose.model('users',usersSchema);
Run Code Online (Sandbox Code Playgroud)
这是帖子架构
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongoosePaginate = require('mongoose-paginate');
var postsSchema = new Schema({
user : { type: Schema.Types.ObjectId, ref: 'users' },
post : String,
created_at : { type : Date, default : Date.now }
});
postsSchema.plugin(mongoosePaginate);
module.exports = mongoose.model('posts',postsSchema); …Run Code Online (Sandbox Code Playgroud) 我的项目中有一个圆圈模型:
var circleSchema = new Schema({
//circleId: {type: String, unique: true, required: true},
patientID: {type: Schema.Types.ObjectId, ref: "patient"},
circleName: String,
caregivers: [{type: Schema.Types.ObjectId}],
accessLevel: Schema.Types.Mixed
});
circleSchema.virtual('caregiver_details',{
ref: 'caregiver',
localField: 'caregivers',
foreignField: 'userId'
});
Run Code Online (Sandbox Code Playgroud)
看护者架构:
var cargiverSchema = new Schema({
userId: {type: Schema.ObjectId, unique: true}, //objectId of user document
detailId: {type: Schema.ObjectId, ref: "contactDetails"},
facialId: {type: Schema.ObjectId, ref: "facialLibrary"}, //single image will be enough when using AWS rekognition
circleId: [{type: Schema.Types.ObjectId, ref: "circle"}], //multiple circles can be present array of …Run Code Online (Sandbox Code Playgroud) Recently asked such a question.
What is faster in Mongo: a populate or individual assync requests?
Example
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var FeedPostCommentSchema = new Schema({
feedPost: {type: Schema.Types.ObjectId, ref: 'FeedPost', index: true},
user: {type: Schema.Types.ObjectId, ref: 'User'},
comment: {
type: String,
trim: true
},
updated: {type: Date, default: Date.now},
created: {type: Date, default: Date.now, index: true}
});
mongoose.model('FeedPostComment', FeedPostCommentSchema);
Run Code Online (Sandbox Code Playgroud)
When displaying all the comments, we also need to get user data
We can make it …
这是我的架构:
var UserSchema = new Schema({
email: String,
name: String,
city: String,
username: String,
profilePic: String,
phoneNo: Number,
shortList: {
project: [{ type: Schema.ObjectId, ref: "Project" }],
flat: [{ type: Schema.ObjectId, ref: "Flat" }],
},
});
var FlatSchema = new Schema({
project: { type: Schema.ObjectId, ref: "Project" },
status: String,
floor: Number,
size: String,
type: String,
flat_number: String,
price: Number,
flooringType: String,
createdAt: { type: Date, 'default': Date.now },
isDeleted: { type: Boolean, 'default': false },
});
var ProjectSchema = …Run Code Online (Sandbox Code Playgroud) 我正在与 mongoose 合作将 ids 字段与他们各自的文档填充到一个新字段中。我的问题是假设我的购物车模型是 -
let CartSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
productIds: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}
]
});
Run Code Online (Sandbox Code Playgroud)
我想填充产品所以我使用
Cart.find({}).populate("products").exec(function (err, cart) {
console.log(cart)
}
Run Code Online (Sandbox Code Playgroud)
但这会以相同的字段名称 productIds 填充文档,我想在名为“products”的新字段名称中填充这些字段,所以我尝试了这个
let CartSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
productIds: [
{
type: String
}
]
}, { toJSON: { virtuals: true } });
CartSchema.virtual('products', {
ref: 'Product',
localField: 'productIds',
foreignField: '_id',
});
Cart.find({}).populate("products").exec(function (err, cart) {
console.log(cart)
} …Run Code Online (Sandbox Code Playgroud) 我遇到了以下我不明白的代码行,尽管有很多教程提供了与示例相关的信息,populate但是没有任何教程可以解释其含义。这里有一个示例
var mongoose = require('mongoose'), Schema = mongoose.Schema
var PersonSchema = new Schema({
name : String,
age : Number,
stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});
var StorySchema = new Schema({
_creator : {
type: Schema.ObjectId,
ref: 'Person'
},
title : String,
fans : [{ type: Schema.ObjectId, ref: 'Person' }]
});
var Story = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
Story.findOne({ title: /Nintendo/i }).populate('_creator') .exec(function (err, story) {
if (err) ..
console.log('The creator is %s', …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的mongo方案,我正在用猫鼬访问
我可以使用populate将用户名和名字映射到每个通知的字段,问题是我似乎无法在日期字段上进行任何排序
使用此代码我得到一个错误
MongooseError:无法在路径notification.from上填充sort,因为它是文档数组的子属性
是否有可能以不同的方式或更新的方式(深度填充,虚拟)这样做?我在使用Mongoose 5.
我宁愿不使用vanilla javascript来对对象进行排序或创建单独的模式
var UserSchema = new Schema({
username: String,
firstname: String,
notifications: [
{
from: { type: Schema.Types.ObjectId, ref: 'User'},
date: Date,
desc: String
}
]
});
app.get('/notifications', function(req, res) {
User.findOne({ _id: req._id }, 'notifications')
.populate({
path: 'notifications.from',
populate: {
path: 'from',
model: 'User',
options: { sort: { 'notifications.date': -1 } }
}
})
.exec(function(err, user) {
if (err) console.log(err)
})
});
Run Code Online (Sandbox Code Playgroud)
关于Mongo,这个可能重复的事实差不多已经有两年了.我问是否有更新或不同的方式在Mongoose中这样做,因为自2016年以来它已经改变了一些新的功能.