N
N
NiceScript2019-06-16 08:43:32
Mongoose
NiceScript, 2019-06-16 08:43:32

How to form a query in Mongoose?

The scheme is

var crm_pixel_Schema = new Schema({
  _id: mongoose.Schema.Types.ObjectId,
  parent_id: {type: Schema.Types.ObjectId, ref: 'category'},
  name: String,
  desc: String,
  order: Number
})

I want to get the largest ordinal number (order) in order to add a new element to the database with order+1.
I need to find the parent categories i.e. whose _id === parent_id
pModel.findOne()
 .sort({order: -1})
 .select({_id: 1, order: 1, parent_id: 1})
 .where('`_id` = `parent_id`')       <===  как вот тут?
 .exec()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NiceScript, 2019-06-16
@NiceScript

pModel.find()
                        .sort({order: -1})
                        .select({_id: 1, order: 1, parent_id: 1})
                        .$where(function () {
                            return this._id.toString() === this.parent_id.toString();
                        })
                        .exec()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question