教程 > mongodb 教程 > 阅读:35

mongodb 插入文档——迹忆客-ag捕鱼王app官网

在本章中,我们将学习如何在 mongodb 集合中插入文档。

insert() 方法

要将数据插入到 mongodb 集合中,我们需要使用 mongodb 的insert()或save()方法。

语法

insert()方法的基本语法如下

>db.collection_name.insert(document)

示例

> db.users.insert({
... _id : objectid("507f191e810c19729de860ea"),
... title: "mongodb overview",
... description: "mongodb is no sql database",
... by: "jiyik",
... url: "http://www.jiyik.com",
... tags: ['mongodb', 'database', 'nosql'],
... likes: 100
... })
writeresult({ "ninserted" : 1 })

这里 users 是我们的集合名称,正如如上一章中创建的那样。如果数据库中不存在该集合,则 mongodb 将创建该集合,然后将文档插入其中。

在插入的文档中,如果我们不指定参数_id,那么mongodb会为这个文档分配一个唯一的objectid。

_id 是 12 字节的十六进制数,对于集合中的每个文档都是唯一的。12 个字节划分如下

_id: objectid(4 bytes 当前时间戳, 3 bytes 主机id, 2 bytes 进程id, 3 bytes 增量)

我们还可以将一组文档传递给 insert() 方法,如下所示:

> db.createcollection("post")
> db.post.insert([
    {
        title: "mongodb overview",
        description: "mongodb is no sql database",
        by: "jiyik",
        url: "http://www.jiyik.com",
        tags: ["mongodb", "database", "nosql"],
        likes: 100
    },
    {
    title: "nosql database",
    description: "nosql database doesn't have tables",
    by: "jiyik",
    url: "http://www.jiyik.com",
    tags: ["mongodb", "database", "nosql"],
    likes: 20,
    comments: [
        {
            user:"user1",
            message: "my first comment",
            datecreated: new date(2013,11,10,2,35),
            like: 0
        }
    ]
}
])
bulkwriteresult({
    "writeerrors" : [ ],
    "writeconcernerrors" : [ ],
    "ninserted" : 2,
    "nupserted" : 0,
    "nmatched" : 0,
    "nmodified" : 0,
    "nremoved" : 0,
    "upserted" : [ ]
})
>

要插入文档,除了insert() 方法之外,也可以使用db.post.save(document)。如果我们没有在文档中指定_id,那么save()方法将与insert()方法相同,自动创建一个唯一的objectid。如果指定了 _id,那么它将替换包含 _id 的整个文档数据,如 save() 方法中指定的那样。


insertone() 方法

如果您只需要将一个文档插入到集合中,则可以使用此方法。

语法

insertone() 命令的基本语法如下

> db.collection_name.insertone(document)

下面的示例创建一个名为 empdetails 的新集合,并使用 insertone() 方法插入一个文档。

> db.createcollection("empdetails")
{ "ok" : 1 }
> db.empdetails.insertone(
    {
        first_name: "radhika",
        last_name: "sharma",
        date_of_birth: "1995-09-26",
        e_mail: "radhika_sharma.123@gmail.com",
        phone: "9848022338"
    })
{
    "acknowledged" : true,
    "insertedid" : objectid("5dd62b4070fb13eec3963bea")
}
>

insertmany() 方法

我们可以使用 insertmany() 方法插入多个文档。对于此方法,需要传递一组文档。

语法

insertmany() 命令的基本语法如下

> db.collection_name.insertmany(documents)

示例

以下示例使用 insertmany() 方法将三个不同的文档插入到 empdetails 集合中。

> db.empdetails.insertmany(
    [
        {
            first_name: "radhika",
            last_name: "sharma",
            date_of_birth: "1995-09-26",
            e_mail: "radhika_sharma.123@gmail.com",
            phone: "9000012345"
        },
        {
            first_name: "rachel",
            last_name: "christopher",
            date_of_birth: "1990-02-16",
            e_mail: "rachel_christopher.123@gmail.com",
            phone: "9000054321"
        },
        {
            first_name: "fathima",
            last_name: "sheik",
            date_of_birth: "1990-02-16",
            e_mail: "fathima_sheik.123@gmail.com",
            phone: "9000054321"
        }
    ]
)
{
    "acknowledged" : true,
    "insertedids" : [
        objectid("5dd631f270fb13eec3963bed"),
        objectid("5dd631f270fb13eec3963bee"),
        objectid("5dd631f270fb13eec3963bef")
    ]
}
>

查看笔记

扫码一下
查看教程更方便
网站地图