【问题标题】:model.find({}).populate('place').populate('location') does not return place and locationmodel.find({}).populate('place').populate('location') 不返回地点和位置
【发布时间】:2017-02-06 22:31:01
【问题描述】:

我在 MongoDb 上有具有以下结构的文档,

我在我的节点应用程序中使用 Mongoose version ^4.8.1。我为上述文档创建了 3 个模式模型,如下所示,

Event.js

var mongoose = require('mongoose');
var eventSchema = new mongoose.Schema({
    description: {
        type: String        
    },
    end_time: {
        type: Date      
    },
    start_time: {
        type: Date      
    },
    name: {
        type: String    
    },
    place: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'place'
    }
});
eventSchema.index({name: 'text'},{'place.location.country':"text"});
var Event = mongoose.model('events', eventSchema);
module.exports= Event;

Place.js

var mongoose = require('mongoose');
var placeSchema = new mongoose.Schema({
    name: {
        type: String    
    },
    location: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'location'
    }
});

var Place = mongoose.model('place', placeSchema);
module.exports= Place;

Location.js

var mongoose = require('mongoose');
var locationSchema = new mongoose.Schema({
    city: {
        type: String        
    },
    latitude: {
        type: String        
    },
    country: {
        type: String        
    },
    located_in: {
        type: String        
    },
    state: {
        type: String        
    },
    street: {
        type: String        
    },
    zip: {
        type: String        
    },


});
var Location = mongoose.model('location', locationSchema);
module.exports= Location;

访问 /query 数据库的通用处理程序,

dbHandler.js

querandpoplulate : function(model,condition,options)
    {
        return new Promise(function(resolve, reject) {
            options = options||{};
            console.log("model is" + model);
            model.find({}).populate('place').populate('location').exec(function(error, data) {
                if (error)
                    console.log(error);
                    reject(error);        
               console.log(data);
                resolve(data);
            })
        })

    }

这是我的查询方式,

dbHelper.querandpoplulate(mongoose.model('events'), {$text: {$search: searchString},'place.location.country': countryString},function(error,data){
                            callback(data);       
});

问题:它不返回带有 place 和 location 的结果集,它在 place 字段中返回 null。

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    在我看来,您的文档保存为embedded documents,但不是referenced documents

    要获取此类文档,您无需执行任何population。简单的find 查询应该适合您。

    试试这个:

    model.find({}).exec(function(error, data) {
        if (error)
            console.log(error);
            reject(error);        
       console.log(data);
        resolve(data);
    })
    

    因为您没有将data 保存在mongoDB 中,而只是检索它。您需要定义与document 结构匹配的schema

    正如你所讨论的,我认为你需要更改Schema,并将所有 3 个schemas 合并到一个文件中(Event.js)。

    var mongoose = require('mongoose');
    var locationSchema = new mongoose.Schema({
        city: {
            type: String        
        },
        //add other fields here 
    });
    
    var placeSchema = new mongoose.Schema({
        name: {
            type: String    
        },
        location: locationSchema
    });
    
    var eventSchema = new mongoose.Schema({
        description: {
            type: String        
        },
        //add other fields here too
        place: placeSchema
    });
    eventSchema.index({name: 'text'},{'place.location.country':"text"});
    var Event = mongoose.model('events', eventSchema);
    module.exports= Event;
    

    【讨论】:

    • 仍然返回 place:null
    • 这是结果 { _id: 5890b47f6166c457ffdee3d3, end_time: 2016-11-11T00:00:00.000Z, name: 'Energía Tour Europa - Milán', place: null, start_time: 2016-11- 10T19:00:00.000Z, id: '631789367000065' }
    • 您的索引 place.location.country':"text" 似乎有误,因为 place 只是该架构中的一个 objectId,而 place.location 将是未定义的。我不知道这是否会导致问题,但其他一切看起来都很好
    • 你检查文档结构了吗?还好吗?
    • 是的,我查看了代码,一切看起来都很好。另外,你能告诉我你的文档是如何存储(或看起来)在 mongoDb 中的吗
    猜你喜欢
    • 1970-01-01
    • 2013-11-23
    • 2013-12-17
    • 2015-08-16
    • 2020-08-24
    • 1970-01-01
    • 2021-07-19
    • 2019-05-29
    相关资源
    最近更新 更多