MongoDB快速入门(四)- 插入文档
插入文档
将数据插入到MongoDB集合,需要使用MongoDB 的 insert() 方法。
语法
insert()命令的基本语法如下:
>db.COLLECTION_NAME.insert(document)
例子
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'yiibai tutorials',
url: 'http://www.yiibai.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
这里 mycol 是我们的集合名称,它是在之前的教程中创建。如果集合不存在于数据库中,那么MongoDB创建此集合,然后插入文档进去。
在如果我们不指定_id参数插入的文档,那么 MongoDB 将为文档分配一个唯一的ObjectId。
_id 是12个字节十六进制数在一个集合的每个文档是唯一的。 12个字节被划分如下:
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
要以单个查询插入多个文档,可以通过文档 insert() 命令的数组方式。
例子
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'yiibai tutorials',
url: 'http://www.yiibai.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database doesn't have tables',
by: 'yiibai tutorials',
url: 'http://www.yiibai.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
查询文档
要从集合查询MongoDB数据,需要使用MongoDB的 find()方法。
语法
find()方法的基本语法如下
>db.COLLECTION_NAME.find()
find() 方法将在非结构化的方式显示所有的文件。 如果显示结果是格式化的,那么可以用pretty() 方法。
语法
>db.mycol.find().pretty()
例子
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "yiibai tutorials",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
除了find()方法还有findOne()方法,仅返回一个文档。
MongoDB快速入门(四)- 插入文档的更多相关文章
- mongodb的基本操作与插入文档(document)
一.mongodb的基本操作: 1.查看mongodb当前所有的databases : show dbs 2.选择数据库(database) : use databaseName(该数据库不存在则会自 ...
- Mongodb(3)插入文档,更新文档,删除文档
insert() 方法 要插入数据到 MongoDB 集合,需要使用 MongoDB 的 insert() 或 save() 方法. 插入文档:db.COLLECTION_NAME.insert(d ...
- MongoDB(四):数据类型、插入文档、查询文档
1. 数据类型 MongoDB支持许多数据类型. 字符串 - 这是用于存储数据的最常用的数据类型.MongoDB中的字符串必须为UTF-8. 整型 - 此类型用于存储数值. 整数可以是32位或64位, ...
- Python MongoDB 插入文档
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- MongoDB 插入文档
文档的数据结构和JSON基本一样. 所有存储在集合中的数据都是BSON格式. BSON是一种类json的一种二进制形式的存储格式,简称Binary JSON. 插入文档 MongoDB 使用 inse ...
- MongoDB 教程(七):插入文档、更新文档、删除文档
MongoDB 插入文档 文档的数据结构和JSON基本一样. 所有存储在集合中的数据都是BSON格式 —— BSON是一种类json的二进制形式的存储格式,简称Binary JSON. MongoDB ...
- MongoDB插入文档
db.collection.insertOne() 插入单个文档.db.collection.insertMany() 插入多个文档.db.collection.insert() 插入单/多个文档. ...
- 【Mongodb教程 第六课 】MongoDB 插入文档
insert() 方法 要插入数据到 MongoDB 集合,需要使用 MongoDB 的 insert() 或 save() 方法. 语法 insert() 命令的基本语法如下: >db.CO ...
- 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...
随机推荐
- 进程通信(socket)
“一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...
- [Spring MVC]学习笔记--DispatcherServlet
在上一篇我们介绍了Servlet,这一篇主要来看一下MVC中用到的DispatcherServlet(继承自HttpServlet). 1. DispatcherServlet在web.xml中被声明 ...
- Introduction to Mathematical Thinking - Week 7
Q: Why did nineteenth century mathematicians devote time to the proof of self-evident results? Selec ...
- puppeteer,webdriverio, chrome webdriver
http://www.r9it.com/20171106/puppeteer.html Puppeteer 至少需要 Node v6.4.0,如要使用 async / await,只有 Node v7 ...
- Docker Metasploit Framework
https://hub.docker.com/r/usertaken/metasploit-framework/ docker pull usertaken/metasploit-framework ...
- MySQL权限系统(二). MySQL提供的特权 Privileges Provided by MySQL
MySQL provides privileges that apply in different contexts and at different levels of operation: Adm ...
- Python是如何进行类型转换的?
函数 描述int(x [,base ]) 将x转换为一个整数long(x [,base ]) 将x转换为一个长整数float(x ...
- javascript实例:显示时间
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python的语法规范及for和while
1.缩进: 空白在Python中是重要的.事实上行首的空白是重要的.它称为缩进.在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组.这意味着同一层次的语句必须有相同的缩进 ...
- 小程序发送 request请求失败 提示不在合法域名列表中的解决方法
可以在小程序开发工具中设置不校验域名.