简体   繁体   中英

How to query documents in mongodb using mongoose

When i try to find a document like const p1 = Profile.find() console.log(p1) i should get all document in that collections but i'm getting different crap. this is my simple schema in mongodb nodejs

    const ProfileSchema = mongoose.Schema({
    name: String,
    age: Number,
    subjects: [String],
    late: Boolean
});

const Profile = mongoose.model('profile',ProfileSchema);

const profile1 = Profile.find()
console.log(profile1)

Use this -

Profile.find({}, function(err, profiles) {
   console.log(profiles);
});

Refer this for more details - https://mongoosejs.com/docs/api.html#model_Model.find

const profile1 = await Profile.find({});

Since Mongoose supports async/await I would make the wrapping function async and call your find function using the await syntax.

Also make a point to pass in an empty object {} to your find function when you want to return all the documents in the collection.

More information here: https://mongoosejs.com/docs/api.html#query_Query-find

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM