繁体   English   中英

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

[英]How to query documents in mongodb using mongoose

当我尝试查找类似const p1 = Profile.find()console.log(p1)的文档时,我应该获取该集合中的所有文档,但是却得到了不同的废话。 这是我在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)

用这个 -

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

请参阅此以获取更多详细信息-https://mongoosejs.com/docs/api.html#model_Model.find

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

由于Mongoose支持async / await,因此我将使包装函数async并使用await语法调用find函数。

当您要返回集合中的所有文档时,还要指出将空对象{}传递给find函数。

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

暂无
暂无

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

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