快递与猫鼬。并在子文档

时间:2017-12-18 23:04:55

标签: node.js mongodb express mongoose

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

国家/地区模型      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
        }
    }
}

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

1 个答案:

答案 0 :(得分: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