Tag: mongoose populate

架构和模型是可以的,mongoose。人口没有工作

我检查了我的数据库forms的terminal和数据是好的,但是当填充'commnets'仍然给我的ID 这是我显示数据的路线 var camp_id= req.params.id ; Campground.findById(camp_id).populate("commnets").exec(function(err,fcg){ if(err) { console.log(err) ;} else { console.log("No err") ; console.log(fcg) ; res.render("show",{campground: fcg});} }) ; moongose schemas(营地与评论) var campgroundSchema = new mongoose.Schema( { name: String , image: String , description : String , comments: [ { type: mongoose.Schema.Types.ObjectId, ref: "Commnet" } ] } ) ; var Campground = […]

填充另一个对象引用的对象

我有一个产品列表,每个产品都有自己的选项。 例如: 蓝色连衣裙(S – L) 红色连衣裙(XS – S – M) 蓝色礼服和红色礼服是产品, S , L , XS , S和M是select。 选项模型有一个产品模型的参考,我想检索所有的产品,然后列出自己的选项。 我只想用一个查询来实现它,我的问题是我从没有链接到它的选项的产品开始。 所以我开始find所有的产品,并与嵌套, 然后与foreach循环,我得到了所有的select。 然后我尝试将选项分配给产品对象(在我的情况下, forEach中的productElem ),但是当我将其从范围当中取出时,它是空的。 如何填充从产品查询开始的选项? 产品架构: var schema = new Schema({ imagePath: {type: String}, title: {type: String, required: true}, description: {type: String, required: true} }); 选项架构: var productOptionSchema = new Schema({ type: {type: String, […]

mongoose:填写一个歧视的子文件

我想填充子文档的字段,这是一个常见模式( Notificationable分为Message或FriendRequest )的区分元素。 这个问题与这个问题非常相似: mongoosejs:从两个不同的模式中填充一个objectId数组 ,这是两年前没有解决的。 由于mongoose进化了,鉴别者也是,我再次提出这个问题。 我到目前为止所尝试的: Notification.find({_id: 'whatever'}) .populate({ path: 'payload', match: {type: 'Message'}, populate: ['author', 'messageThread'] }) .populate({ path: 'payload', match: {type: 'FriendRequest'}, populate: ['to', 'from'] }) .exec(); 这是行不通的,因为path是一样的。 所以我试了一下: Notification.find({_id: 'whatever'}) .populate({ path: 'payload', populate: [ { path: 'messageThread', match: {type: 'Message'}, }, { path: 'author', match: {type: 'Message'}, }, { […]

mongoose虚拟填充和聚合

我正在尝试使用更新的虚拟填充function(使用Mongoose 4.13,Mongo 3.6)的Mongoose模式进行聚合。 可以说,我有以下(为了说明目的简化)模式: const ProjectSchema = new mongoose.Schema({ projectId: Number, description: String }); ProjectSchema.virtual('tasks', { ref: 'Task', localField: 'projectId', foreignField: 'projectId' justOne: false ]); const TaskSchema = new mongoose.Schema({ taskId: Number, projectId: Number hours: Number }); const Project = mongoose.model('Project', ProjectSchema); const Task = mongoose.model('Task', TaskSchema); 查询项目和填充相关的任务是正常使用.populate()如: Project.find({projectId: <id>}).populate('tasks'); 但是现在我想总结一下按项目完成任务的时间(把$ sum部分留在下面)。 无论如何,我只能得到空arrays。 是不是可以与虚拟填充聚合? const […]

mongoose:三路文件join

我正在学习mongoose,需要一些帮助。 我有3个集合,并且在一个API调用中,我想创build3个互相引用的文档; “join”如下: 用户 – 需要引用chirps video – 需要引用chirps 啁啾声 – 需要引用用户和唧唧声 问题:我知道我可以做一个model.create(),并在每个callback中传递新文档,然后更新到相应的文档,但我想知道是否有一个更干净的方法来做到这一点? 对不起,如果我不清楚的问题。 请问我是否有意义。 码 var chirpSchema = new mongoose.Schema({ date_created: { type: Date, default: Date.now } , content: { post : String } , _video: { type: $oid, ref: "video" } , _author: { type: $oid, ref: "user" } }); var chirp = […]

无法在Mongoose / MongoDB中使用坐标$ near语句find附近位置的结果

我正试图从一个特定的点获得附近的位置的结果。 通过使用ID查找的正常方式,返回的结果,但是当我试图find使用坐标附近的位置,没有结果出现。 在我的代码中没有任何错误/警告,但也没有任何结果。 我错过了什么? 模型模式 var locationSchema = new Schema({ fb_location: String, coordinates:[] }); var feed_postSchema = new Schema({ content: String, location: [locationSchema], image: [{ type : String }], created_at: { type : Date, default: Date.now } }); locationSchema.index({coordinates:'2dsphere'}); Server.js app.get('/test_radar',function(request,response){ Feed_Post.find( { 'location.coordinates' : { $near : { coordinates: [100,5] }, $maxDistance: 100 } […]

mongoose不能使用typesstring填充

所以我有这个FacilityPersonnel模型的字段: var field = { createdAt: {type: Date, default: Date.now()}, updatedAt: {type: Date, default: Date.now()}, some_id: {type: String}, fooAccessType: { type: String, default: 'SuperAdmin', ref: 'AccessType' }, } 我试图在控制器上使用这个populate fooAccessType 。 FacilityPersonnel.findOne({_id:req.params.id}) .populate('fooAccessType') .exec(function (err, doc) { if (err) { res.status(500).json(err); return; }; res.status(200).json(doc); }) 所有要查询的数据都是可用的,并且当我删除行.populate('fooAccessType')时查询正在工作,但如果没有,我会返回这个错误: { "stack": "Error\n at MongooseError.CastError …", "message": "Cast to […]

如何使用node.js中的数据从两个表中search数据?

我有下面的架构用户和报告请求表,并使用人口获取和显示报告用户列表。 var user = new Schema({ name : {type:String,required:[true,"name is required"]}, }); var report_request = new Schema({ user_id : {type:Schema.Types.ObjectId, ref: 'user' }, reported_by_id : {type:Schema.Types.ObjectId, ref: 'user' }, reason : String, }); 但问题是我有在列表中的searchfilter,并可以通过名称search报告的用户。 所以我想要做这样的事情:report_request.find({'user.name':/ Ruby / i}); id Name Reported By 1 Ruby Mark 2 Johny Ruby 我试图做到这一点,但它不工作。 那么他们有什么其他的方式来做到这一点? 提前致谢!!

使用mongoose Node Js对来自不同集合的字段进行sorting

我有两个集合: Person schema { _id : Number, name : String, age : Number, stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }] } Story schema { _creator : { type: Number, ref: 'Person' }, title : String, fans : [{ type: Number, ref: 'Person' }] } 我正在使用以下查询从“ Story ”集合中获取数据,但是我希望数据应该按照“ Person ”集合的“ name ”字段进行sorting: Story.find({"title" : "education"}).populate('fans').populate('_creator').exec() […]

Mongoose:用投影填充对象的嵌套数组

var CarSchema = new Schema({ name: {type: String}, partIds: [{type: Schema.Types.ObjectId, ref: 'Part'}], }); var PartSchema = new Schema({ name: {type: String}, props: [ { colour: {type: String}, shape: {type:String} } ], }); 例如 Car = { name: "BMW", partIds:[ObjectId("57baa43e152654f80aac36a6")]} Part = { _id: ObjectId("57baa43e152654f80aac36a6"), name: "Piston", props: [{colour:"red", shape: "Cubical"},{colour:"green", shape: "cylindrical"}] 所以,当我查询我应该得到这样的文档: Car […]