【问题标题】:Expressjs with mongoose. and fetching foreignkey subdocument inside subdocumentsExpressjs 与猫鼬。并在子文档中获取外键子文档
【发布时间】:2018-06-01 08:13:00
【问题描述】:

我有使用外键与国家相关的国家。在获取国家时,我无法获得相关国家,但得到一个奇怪的结果。以下是我的模型

国家模式 var 猫鼬 = 要求('猫鼬'); var Schema=mongoose.Schema

var CountrySchema =Schema(
    {
        name:{type:String,required:true},
        code :{type:String},
        status: { type: Boolean, default:true }
    }
)


    module.exports = mongoose.model('Country', CountrySchema

)

状态模型

    var mongoose = require('mongoose');
var Schema=mongoose.Schema

var Country =require('../../models/location/country');



var StateSchema=Schema(
    {
        name:{type:String,required:true},
        code :{type:String},
        status: { type: Boolean, default:true },
        country: { type: Schema.ObjectId, ref: 'Country', required: true },
        country:[Country.schema]
    }
)

module.exports = mongoose.model('State', StateSchema)

我的回复如下

{
    "data": {
        "5a36f32fc6ac751b711e9b19": {
            "name": "Kerala",
            "code": "KL",
            "_id": "5a36f32fc6ac751b711e9b19",
            "__v": 0,
            "country": [
                {
                    "_bsontype": "ObjectID",
                    "id": {
                        "type": "Buffer",
                        "data": [
                            90,
                            47,
                            16,
                            175,
                            152,
                            157,
                            109,
                            20,
                            5,
                            111,
                            70,
                            142
                        ]
                    },
                    "status": true
                }
            ],
            "status": true
        }
    }
}

为什么我获取国家/地区的这种类型数据而不是获取正确的数据

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    您正在向我们展示可能存在其他问题的 api 响应。我纯粹在猫鼬层上回答你的问题。您将需要编辑您的StateSchema,因此country 不会被定义两次。然后使用$looup

    const Schema = require('mongoose').Schema;
    const mongoose = require('mongoose');
    
    const CountrySchema = Schema({
        name: { type: String, required: true },
        code: { type: String },
        status: { type: Boolean, default: true }
    });
    const Country = mongoose.model('Country', CountrySchema, 'countries');
    
    const StateSchema = Schema({
        name: { type: String, required: true },
        code: { type: String },
        status: { type: Boolean, default: true },
        country: { type: Schema.ObjectId, ref: 'Country', required: true }
    });
    const State = mongoose.model('State', StateSchema, 'states');
    
    mongoose.connect('mongodb://localhost:27017/test');
    
    mongoose.connection.once('open', async () => {
        console.log("Connected to db.");
        const australia = await Country.insertMany({
            name: 'Australia',
            code: 'AU',
            status: 'active'
        })
    
        await State.insertMany({
            name: 'New South Wales',
            code: 'NSW',
            status: 'active',
            country: australia[0]._id
        })
    
    const join = await State.aggregate([
        {
            $lookup: {
                from: 'countries',
                localField: 'country',
                foreignField: '_id',
                as: 'country_docs'
            }
        }
        ]);
        console.log(JSON.stringify(join));
    });
    

    $lookup is only available in mongo > 3.2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 2019-10-06
      • 1970-01-01
      • 2017-11-24
      • 2017-05-07
      • 2015-06-26
      • 2015-10-01
      • 2019-06-06
      相关资源
      最近更新 更多