MongoDB 插入文档
本章节中我们将向大家介绍如何将数据插入到MongoDB的集合中。
文档的数据结构和JSON基本一样。
所有存储在集合中的数据都是BSON格式。
BSON是一种类json的一种二进制形式的存储格式,简称Binary JSON。
插入文档
MongoDB 使用 insert() 或 save() 方法向集合中插入文档,语法如下:
db.COLLECTION_NAME.insert(document)
实例
以下文档可以存储在 MongoDB 的 codercto 数据库 的 col 集合中:
>db.col.insert({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: '码农教程',
url: 'http://www.codercto.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
以上实例中 col 是我们的集合名,如果该集合不在该数据库中, MongoDB 会自动创建该集合并插入文档。
查看已插入文档:
> db.col.find()
{ "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "码农教程", "url" : "http://www.codercto.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>
我们也可以将数据定义为一个变量,如下所示:
> document=({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: '码农教程',
url: 'http://www.codercto.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
});
执行后显示结果如下:
{
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "码农教程",
"url" : "http://www.codercto.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
执行插入操作:
> db.col.insert(document)
WriteResult({ "nInserted" : 1 })
>
插入文档你也可以使用 db.col.save(document) 命令。如果不指定 _id 字段 save() 方法类似于 insert() 方法。如果指定 _id 字段,则会更新该 _id 的数据。
点击查看所有 MongoDB 教程 文章: https://codercto.com/courses/l/32.html
Web Security Testing Cookbook
Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99
Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!
HTML 编码/解码
HTML 编码/解码
html转js在线工具
html转js在线工具