简体   繁体   English

如何使用猫鼬在mongodb中查询文档

[英]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. 当我尝试查找类似const p1 = Profile.find()console.log(p1)的文档时,我应该获取该集合中的所有文档,但是却得到了不同的废话。 this is my simple schema in mongodb nodejs 这是我在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 请参阅此以获取更多详细信息-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. 由于Mongoose支持async / await,因此我将使包装函数async并使用await语法调用find函数。

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. 当您要返回集合中的所有文档时,还要指出将空对象{}传递给find函数。

More information here: https://mongoosejs.com/docs/api.html#query_Query-find 此处的更多信息: https : //mongoosejs.com/docs/api.html#query_Query-find

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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