findById查询只返回_id字段

时间:2018-04-23 21:43:52

标签: node.js mongodb mongoose

我有这个问题:

  return Role.findById(_id, {lean: true}).exec().then(function (role: any) {

    if (!(role && role.key && role._id)) {
      log.error('role doc was missing the "key"/"_id" field =>', role);
      return;
    }

    caches.RolesCache[role.key] = role;
    caches.RolesCacheById[role._id] = role;
  });

我记录了这个:

role doc was missing the "key"/"_id" field => { _id: 5ade466c1384a4deffac6b09 }

为什么findById函数只返回文档的_id字段?太奇怪了。我发誓这曾经工作得很好。对于我们的其他类似模型而言,正在发生这种情况。

请注意,我也尝试过:

Role.findById(_id).lean().exec()

Role.findById(_id).exec()

Role.findOne({_id}).exec()

并且有同样的问题。 当我到达shell时,我跑了:

rs0:PRIMARY> db.roles.find({_id:ObjectId("5ade466c1384a4deffac6b09")})
{ "_id" : ObjectId("5ade466c1384a4deffac6b09"), "active" : true, "functionalGroup" : "IT", "defaultCategories" : [ "CATEGORY_GENERAL", "CATEGORY_NETWORK_LANDSCAPE", "CATEGORY_CALL_CENTER", "CATEGORY_TELECOMMUNICATIONS", "CATEGORY_VIDEO" ], "key" : "ROLE_XXX", "name" : "ROLE_XXX", "createdBy" : "alexamil", "createdAt" : ISODate("2018-04-23T20:47:40.413Z"), "updatedBy" : "alexamil", "updatedAt" : ISODate("2018-04-23T20:47:40.413Z"), "__v" : 0 }

任何人都知道发生了什么事?

  

我使用的是Mongoose版本:5.0.16和Mongo版本:3.4.x

Role的架构定义是:

'use strict';

import {Schema, Document, model, Model} from 'mongoose';
import {CDTBase, CDTBaseModel} from "../base-model";

/////////////////////////////////////////////////////////////////////

const rolesSchema = new Schema(Object.assign({

  _id: {
    type: Schema.Types.ObjectId,
    required: true,
    auto: true
  },

  key: {
    // the canonical identifier (all uppercase, etc)
    type: String,
    required: true
  },

  name: {
    // the display name
    type: String,
    required: true
  },

  active: {
    // if we wish to deactivate this role, we would have to
    // change all users with this role to a different existing role
    type: Boolean,
    default: true
  },

  functionalGroup: {
    // functional group key
    type: String,
    // required: true,
    default: 'IT'
  },

  defaultCategories: {
    // default category keys
    type: [String],
    // required: true,
    default: []
  },

  persona: String,

}, CDTBase));


rolesSchema.index({name: 1}, {unique: true, background: false});
rolesSchema.index({key: 1}, {unique: true, background: false});

export interface CDTModel extends CDTBaseModel, Document{
  key: string,
  name: string,
  active: boolean,
  functionalGroup: string,
  defaultCategories: Array<string>,
  persona: string
}


export const cdtmodel = model<CDTModel>('Role', rolesSchema, 'roles');

base-model.ts看起来像:

export interface CDTBaseModel {
  createdAt?: Date,
  updatedAt?: Date,
  updatedBy?: string,
  createdBy?: string
}


export const CDTBase = {

  createdBy: {
    type: String,
    required: false
  },

  updatedBy: {
    type: String,
    required: false
  },

  createdAt: {
    type: Date,
    required: false
  },

  updatedAt: {
    type: Date,
    required: false
  }

};

2 个答案:

答案 0 :(得分:1)

更改查询
Role.findById(_id, {lean: true})

Role.findById(_id).lean()

根据their docs,第二个参数是投影,它错过了key

答案 1 :(得分:0)

请尝试这个

Role.findById({_id},function (err,role) {

    if (err) throw err;
    if(!role){console.log("role not found")}
else{
    caches.RolesCache[role.key] = role;
    caches.RolesCacheById[role._id] = role;
}
  });