Pass mongoDB query to Mongoose

0

I need to pass the following MongoDb query to Mongoose:

db.propiedads.find({
  _id:ObjectId("5bb18cc42a8f2d23d8dfcd65"),
  "planes.plan":ObjectId("5bb565f6382cf30364b182e5")
 })

Currently this query is working in the mongo console. now I add the mongoose model that represents the collection in which I made the query:

'use strict'
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var tipos = ['CASA','DEPARTAMENTO','EMPRESA','CONDOMINIO'];

var PropertySchema = Schema({
  tipo:{
    type:String,
    enum:{values:tipos, message:'Tipo de propiedad no valida'}
  },
  direccion: {
    comuna: {
      type:String,
      required:true
    },
    calle:{
      type:String,
      required:true
    },
    numero:{
      type:Number,
      required:true
    },
    departamento: String},
  id_usuario:{
    type: Schema.ObjectId,
    ref:'Usuario',
    required:true
    },
  fecha_inscripcion:Date,
  fecha_actualización:Date,
  planes:[
    {
      plan:{
        type: Schema.ObjectId,
        ref:'Plan',
        _id : false
      },
        create:Date
      }],
});

module.exports = mongoose.model('Propiedad' ,PropertySchema);

I hope you can help me, thank you very much anyway.

    
asked by Miguel Angel Gonzalez Jaimen 11.10.2018 в 04:12
source

1 answer

0

In user_id.type and plans.plan.type it should be Schema.Types.ObjectId instead of Schema.ObjectId

Try the following:

Propiedad.find({
  _id: "5bb18cc42a8f2d23d8dfcd65",
  "planes.plan": "5bb565f6382cf30364b182e5"
});
    
answered by 25.10.2018 / 17:46
source