快递与猫鼬。并在子文档中获取foreignkey子文档

问题描述 投票:0回答:1

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

国家模型var mongoose = require('mongoose'); 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个回答
0
投票

您向我们展示了可能存在其他问题的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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.