Mongodb не возвращает поле _id в find при использовании mon goose -auto-increment - PullRequest
0 голосов
/ 09 февраля 2020

Схема

------------------------Country Schema :----------------------
let CountrySchema = new mongoose.Schema({  
name: { type: String, required: true, unique: true },
states: [{ type: Number, ref: "State" }]
});

// Auto increment id:
autoIncrement.initialize(mongoose.connection);
CountrySchema.plugin(autoIncrement.plugin, {
  model: "Country",
  field: "_id",
  startAt: 1,
  incrementBy: 1
});

-----------------------States Schema: ------------------------------------
let StateSchema = new mongoose.Schema({  
  name: { type: String, required: true, unique: true },
  districts: [{ type: Number, ref: "District" }],
  parent : {type:Number , ref:"Country"}   
});
// Auto increment id:
autoIncrement.initialize(mongoose.connection);
StateSchema.plugin(autoIncrement.plugin, {
  model: "State",
  field: "_id",
  startAt: 1,
  incrementBy: 1
});

-------------------------Query:-----------------
states.find({}}.then(data=>{ console.log(data) })
----------------Console Result: -------------------------
[
  { districts: [], name: 'x', parent: 1, __v:
0 }
]
--------------Actual values in database -----------------

    State =>  { _id:{ $oid: 12ByteobjectID_here } ,districts: [], name: 'x', parent: 1, __v:0 }
   Country => { _id:1 ,states:[], name: 'x', __v:0 }
-------------------------------------------------

Как вы можете видеть выше, результат запроса для модели состояния не возвращает _id, однако в базе данных я вижу поле в формате _id {$ oid : 12byteValueHere}.

Также я не понимаю, почему поле _id документов модели страны хранится как _id: 1 вместо _id {$ oid: 12byte}, как схема состояний.

в обеих схемах есть промежуточное ПО с автоматическим приращением, почему _id хранится по-разному

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...