Mongoose坚持ref不工作也不更新模型的属性

时间:2017-11-16 16:03:31

标签: node.js mongodb mongoose

我在MongoDB中使用Mongoose,我在下面的代码中(1)检索客户端模型,然后(2)将其与新的范围模型相关联问题是当我坚持我的范围模型时,它不会保存与客户的关联:

await connectionManager.connect();
let client = await connectionManager.getClient().findOne({'client' : 'local'});
let grant = await connectionManager.getGrant().findOne({'name' : 'Client_Credential'});
let service = await connectionManager.getService().findOne({'description' : 'Service 1'});
let ScopeModel = connectionManager.getScope();
let scope = new ScopeModel({client_id : client._id, grant_id : grant._id, service_id : service._id});
await scope.save();

console.log(await connectionManager.getClient().findOne({'client' : 'local'}).populate('scopes'));
console.log(await connectionManager.getScope().findOne({}));

这是console.log行打印出来的:

 { _id: 5a0706cc26b1572d945811b4,
  name: 'Blah LTD',
  address: '',
  city: '',
  country: 'Unknown',
  telephone: '',
  owner: true,
  url: '',
  client: 'local',
  client_secret: 'secret',
  email: 'email' }


{ _id: 5a0e870a2f013725402ea5dd, __v: 0 }

保存后,MongoDB中的范围集合中显示的内容相同或多或少:

_id: 5a0e870a2f013725402ea5dd
__v: 0 

不确定__v:0代表什么,但无论如何,模式如下所示:

客户端收集:

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
});

ClientSchema.virtual("scopes", {
    ref : 'Scope',
    localField : '_id',
    foreignField : 'client_id',
    justOne: false
});

export default ClientSchema;

我的范围集

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

var ScopeSchema = Schema({
    client_id: String,
    service_id: String,
    grant_id: String
});

ScopeSchema.virtual("client", {
    ref : 'Client',
    localField : 'client_id',
    foreignField : '_id',
    justOne: true
});

ScopeSchema.virtual("service", {
    ref : 'Service',
    localField : 'service_id',
    foreignField : '_id',
    justOne: true
});

ScopeSchema.virtual("grant", {
    ref : 'Grant',
    localField : 'grant_id',
    foreignField : '_id',
    justOne: true
});

export default ScopeSchema;

这是我的connectionManager方法:

import MongoClient from 'mongoose';
import {ClientModel, GrantModel, ServiceModel, ScopeModel } from '../schemas';

class ConnectionManager {
    constructor() {
        this.mongoClient = MongoClient;
        this.mongoClient.Promise = Promise;
    }
    async connect() {
        await this.mongoClient.connect(process.env.MONGO_URL, {'useMongoClient' : true});
        this.client = this.mongoClient.model('Client', ClientModel);
        this.grant = this.mongoClient.model('Grant', GrantModel);
        this.service = this.mongoClient.model('Service', ServiceModel);
        this.scope = this.mongoClient.model('Scope', ScopeModel);
    }
    getDB () {
        return this.mongoClient;
    }
    getClient()
    {
        return this.client;
    }
    getGrant()
    {
        return this.grant;
    }
    getService()
    {
        return this.service;
    }
    getScope()
    {
        return this.scope;
    }
}
export default new ConnectionManager();

似乎没有任何东西可以与Mongoose一起使用,所以我尽量尝试更新client模型:

let client = await connectionManager.getClient().findOne({'client' : 'local'});
console.log(client);
client.name = "Hello World";
client.save();
console.log(await connectionManager.getClient().findOne({'client' : 'local'}));

运行client.name = "Hello World";之前的console.log是这样的:

{ _id: 5a0706cc26b1572d945811b4,
  name: 'Blah LTD',
  address: '',
  city: '',
  country: 'Unknown',
  telephone: '',
  owner: true,
  url: '',
  client: 'local',
  client_secret: 'secret',
  email: 'email' }

更新client.name = "Hello World"; client.save();后的console.log是:

{ _id: 5a0706cc26b1572d945811b4,
      name: 'Blah LTD',
      address: '',
      city: '',
      country: 'Unknown',
      telephone: '',
      owner: true,
      url: '',
      client: 'local',
      client_secret: 'secret',
      email: 'email' }

正如您所看到的,client.name未更新为Hello World

0 个答案:

没有答案