mongoose-elasticsearch-xp icon indicating copy to clipboard operation
mongoose-elasticsearch-xp copied to clipboard

esSynchronize with populate

Open vavarun opened this issue 7 years ago • 0 comments
trafficstars

Currently this is my setup and I wish to store a value from Invoice mongoose collection to Transaction Elastic document using populate at esSynchronize. I used es_extend to map a new property invoice_number.

const TransactionSchema = new mongoose.Schema(
  {
    currency: { type:  Number },
    amount: { type: Number },

    invoice_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Invoice' },
  },
  {
    timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
    es_extend: {
      invoice_number: {
        es_type: 'string',
        es_value: ''
      },
    },
  }
);

Transaction.find({})
 .populate('invoice_id', 'invoice_number')
 .lean()
 .then((res) => {
   const query = res.map((transaction) => {
     transaction.invoice_number = transaction.invoice_id.invoice_number;
     transaction.invoice_id = transaction.invoice_id._id;
     return transaction;
   });
   console.log(query);
   Transaction.esSynchronize(query)
     .then(() => console.log('Transaction sync end.'))
     .catch((err) => {
       console.log(err);
     });
 });

The query is populated correctly but when I check after the esSync is complete, invoice_number field is still an empty string in the ES document. If someone could assist me

vavarun avatar Jun 05 '18 19:06 vavarun