Wykonałem ten kod

 db.courses.find({isPublished: true , tags: "backend"} , {name: 1 , author:1}).sort({name: 1})

W Mongodb Shell i dał mi wymagane wyjście. Teraz próbuję wykonać ten sam kod używający mangusta i daje mi to

[
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: [Object],
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5a68fde3f09ad7646ddec17e,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      _id: 5a68fde3f09ad7646ddec17e,
      name: 'ASP.NET MVC Course',
      author: 'Mosh'
    },
    '$init': true
  },
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: [Object],
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5a68fdc3615eda645bc6bdec,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      _id: 5a68fdc3615eda645bc6bdec,
      name: 'Express.js Course',
      author: 'Mosh'
    },
    '$init': true
  },
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: [Object],
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5a68fdd7bee8ea64649c2777,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      _id: 5a68fdd7bee8ea64649c2777,
      name: 'Node.js Course',
      author: 'Mosh'
    },
    '$init': true
  },
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: [Object],
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5a68fe2142ae6a6482c4c9cb,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      _id: 5a68fe2142ae6a6482c4c9cb,
      name: 'Node.js Course by Jack',
      author: 'Jack'
    },
    '$init': true
  }
]

Chociaż daje mi wymagane dane wyjściowe wraz z dodatkowymi nie zrozumiałymi rzeczami, które sprawia, że nie odczytany

 const mongoose = require("mongoose"); 


const fun = async() => {

    await mongoose.connect("mongodb://localhost:27017/Exercise"); 

    const schema  = mongoose.Schema({
        name: String , 
        author: String , 
        tags: [String] ,
        date: Date , 
        isPublished: Boolean , 
        price: Number 
    }); 

    const Course = mongoose.model("Course" , schema); 

    const result = await Course.find({isPublished: true
        , tags: "backend"})
        .sort({name: 1 })
    .select({name: 1  , author: 1 }); 

    console.log(result); 

}
fun();
0
Ashish Ashok Kumar Singh 26 luty 2021, 12:20

1 odpowiedź

Najlepsza odpowiedź

Musisz konwertować model Mongoose do obiektu.

Możesz konwertować na obiekt za pomocą funkcji Lean ().

const result = await Course.find({isPublished: true
        , tags: "backend"})
        .sort({name: 1 })
    .select({name: 1  , author: 1 })
.lean()

Ponadto można to zrobić w inny sposób, jak,

let result = await Course.find({isPublished: true
        , tags: "backend"})
        .sort({name: 1 })
    .select({name: 1  , author: 1 })
result = result.map(r => r.toObject())
0
Vikas Keskar 26 luty 2021, 09:24