Expressjs 与猫鼬。并在子文档中获取外键子文档

Posted

技术标签:

【中文标题】Expressjs 与猫鼬。并在子文档中获取外键子文档【英文标题】:Expressjs with mongoose. and fetching foreignkey subdocument inside subdocuments 【发布时间】: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
        
    

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

【问题讨论】:

【参考方案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

【讨论】:

以上是关于Expressjs 与猫鼬。并在子文档中获取外键子文档的主要内容,如果未能解决你的问题,请参考以下文章

在 Hibernate 中,如何自动检索父 id 并在子插入中将其用作外键?

NodeJS-ExpressJS 猫鼬 API POST 请求与外键

获取计数并与猫鼬中的响应合并

将模糊搜索与猫鼬一起使用时出现打字稿错误

将模糊搜索与猫鼬一起使用时出现打字稿错误

如何在子文档中填充模型实例数组? MongoDB猫鼬