Mongoose populate方法不起作用

时间:2017-11-16 09:02:14

标签: mongodb mongoose

美好的一天:

使用mongoose填充范围路径时出现问题。这是我的客户端的集合:

[
    {
        "_id": { "$oid": "5a0706cc26b1572d945811b4" },
        "name": "DFFDAFDAFA",
        "address": "",
        "city": "",
        "country": "UNKNOWN",
        "telephone": "",
        "owner": true,
        "url": "",
        "client": "local",
        "client_secret": "DFDFADFAFD",
        "email": "email"
    }
]

这是我的范围的集合:

[
    {
        "client": "5a0706cc26b1572d945811b4",
        "service": "5a0707adbd0bea2dbf02daf9",
        "grant" : "5a074b7e3eba70336e172ddc"
    }
]

以下代码是我用于查询的代码,但范围路径未填充:

async getClient (clientId, clientSecret, callback) {
        let client = await this.connection.getClient().findOne({"client" : clientId, "client_secret" : clientSecret}).populate('scope');
        console.log(client);
    }

以上代码的输出是:

{ _id: 5a0706cc26b1572d945811b4,
  name: 'DFFDAFDAFA',
  address: '',
  city: '',
  country: 'UNKNOWN',
  telephone: '',
  owner: true,
  url: '',
  client: 'local',
  client_secret: 'DFDFADFAFD',
  email: 'email' }

这些是架构:

Client Schema:

import mongoose from 'mongoose';
let Schema = mongoose.Schema;

let ClientSchema = Schema({
    name: String,
    address: String,
    city: String,
    country: String,
    telephone: String,
    owner: Boolean,
    url: String,
    client: String,
    scopes : [{ type: Schema.Types.ObjectId, ref: 'Scope' }]
});

export default ClientSchema;

Scope Schema:

import mongoose from 'mongoose';
let Schema = mongoose.Schema;

var ScopeSchema = Schema({
    client: [{ type: Schema.Types.ObjectId, ref: 'Client' }],
    service: [{ type: Schema.Types.ObjectId, ref: 'Service' }],
    grant: [{ type: Schema.Types.ObjectId, ref: 'Grant' }]
});

export default ScopeSchema;

0 个答案:

没有答案