关于MongoDB的全面概述和我所有关于它的帖子,请查看我的概述。
MongoDB有几种将文档插入到集合中的方法。主要的方法是在集合中使用一种叫做insert. 的方法,下面是一个例子:
// Let's say we want to insert a new document representing a new user
db.users.insert({
email: "test@test.com",
username: "test_user",
password: "please encrypt your passwords"
})
这将把我们的 "用户 "文档插入到用户集合中。我们甚至不需要确保users集合已经存在,因为MongoDB的集合在插入过程中会自动创建。你也可以使用insert 方法在同一时间向MongoDB集合中插入许多文档。
较新版本的MongoDB在使用insert 时会给你一个弃用警告,而希望你使用insertOne 来插入单个文档:
db.users.insertOne({
email: "test@test.com",
username: "test_user",
password: "please encrypt your passwords"
})