我正在尝试学习MongoDB
. 我已经用Compass
创建了一个Collection
,但我不知道如何添加索引和字段,如name
,gender
.。等,到创建的集合。
如果有人能帮助我,我将不胜感激。
了解您在哪个上下文中使用MongoDB会有所帮助。
您需要通过创建定义集合中文档属性的架构来对数据进行建模。
下面是一个使用 Express 和猫鼬的快速示例:
const mongoose = require('mongoose');
var commentSchema = new mongoose.Schema({
text: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
date: {
type: Date,
default: Date.now()
},
likes: [
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
}
]
});
module.exports = mongoose.model("Comment", commentSchema);