Node.js 如何使用mongoose populate在多个案例中仅返回一个客户端?

Node.js 如何使用mongoose populate在多个案例中仅返回一个客户端?,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我正在使用mongoose填充来尝试在我的模式中创建一对多关系 var clientSchema = new mongoose.Schema({ name: { type: String, required: true }, title: { type: String, default: "N/S" }, birthDate: { type: Date }, ssn: { type: String, default: "N/S" }, spouse: {

我正在使用mongoose填充来尝试在我的模式中创建一对多关系

var clientSchema = new mongoose.Schema({
    name:  { type: String, required: true },
    title: { type: String, default: "N/S" },
    birthDate: { type: Date },
    ssn: { type: String, default: "N/S" },
    spouse: { type: String, default: "N/S" },
    notes: { type: String, default: "N/S" },
    address: { type: String, default: "N/S" },
    city: { type: String, default: "N/S" },
    state: { type: String, default: "N/S" },
    zip: { type: String, default: "N/S" },
    homePhone: { type: String, default: "N/S" },
    workPhone: { type: String, default: "N/S" },
    mobilePhone: { type: String, default: "N/S" },
    fax: { type: String, default: "N/S" },
    email: { type: String, default: "N/S" }
});
var caseSchema = mongoose.Schema({
    _client: { type: mongoose.Schema.Types.ObjectId, ref: 'Client' },
    name: { type: String, required: true },
    lead: { type: String },
    priority: { type: String },
    dateOpened: { type: Date },
    dateAccident: { type: Date },
    status: { type: String },
    sol: { type: Date },
    description: { type: String }
});
我希望能够查询数据库,以获取具有给定客户id的所有案例。 我有这个工作,但不是我想要的方式。现在,当我使用populate方法时 在我的路线中,我得到了所有带有客户id的案例,但我也得到了 所有客户端,即使所有客户端都完全相同。对于每个案例,返回相同的客户端似乎是浪费资源。有没有办法只返回客户一次,然后返回所有相关案例

app.get('/cases/:id', function( req, res ) {
    Case
    .find( { _client: req.params.id } )
    .populate('_client')
    .exec( function( err, cases ) {
        res.send( cases );
    });
});
以下是我得到的反馈:

[
    {
        "_client": {
            "name": "John Doe",
            "birthDate": null,
            "_id": "51705a7ed0ecd0a906000001",
            "__v": 0,
            "email": "",
            "fax": "",
            "mobilePhone": "",
            "workPhone": "",
            "homePhone": "",
            "zip": "",
            "state": "",
            "city": "",
            "address": "",
            "notes": "",
            "spouse": "",
            "ssn": "",
            "title": "Mr"
        },
        "name": "test",
        "lead": "",
        "priority": "",
        "dateOpened": null,
        "dateAccident": null,
        "status": "",
        "sol": null,
        "description": "",
        "_id": "5170679df8ee8dd615000001",
        "__v": 0
    },
    {
        "_client": {
            "name": "John Doe",
            "birthDate": null,
            "_id": "51705a7ed0ecd0a906000001",
            "__v": 0,
            "email": "",
            "fax": "",
            "mobilePhone": "",
            "workPhone": "",
            "homePhone": "",
            "zip": "",
            "state": "",
            "city": "",
            "address": "",
            "notes": "",
            "spouse": "",
            "ssn": "",
            "title": "Mr"
        },
        "name": "newest case",
        "lead": "",
        "priority": "",
        "dateOpened": null,
        "dateAccident": null,
        "status": "",
        "sol": null,
        "description": "",
        "_id": "517067d8806f060b16000001",
        "__v": 0
    },
    {
        "_client": {
            "name": "John Doe",
            "birthDate": null,
            "_id": "51705a7ed0ecd0a906000001",
            "__v": 0,
            "email": "",
            "fax": "",
            "mobilePhone": "",
            "workPhone": "",
            "homePhone": "",
            "zip": "",
            "state": "",
            "city": "",
            "address": "",
            "notes": "",
            "spouse": "",
            "ssn": "",
            "title": "Mr"
        },
        "name": "adding new case",
        "lead": "Me",
        "priority": "Urgent",
        "dateOpened": null,
        "dateAccident": null,
        "status": "",
        "sol": null,
        "description": "",
        "_id": "51709a16806f060b16000002",
        "__v": 0
    }
]
这似乎不适合将所有这些膨胀发送到我的视图进行渲染。我是否应该像这样使用一对多填充?
除了嵌入式文档,我在mongoose.com上看到的所有示例都是一对一的。

如果您不想在每个案例中重复客户端,最好分别查询客户端,然后将该结果与
案例
查询的结果相结合(无
填充
)以你需要的任何方式


顺便说一句,
“/cases/:id”
是一个相当混乱的URL,用于按客户id而不是案例id获取案例。

我意识到这似乎有点混乱。为了测试的目的,我很快就把它组装起来了。不过我会试试看。