mongodb笔记 getting started

基本概念

数据库名全部小写
MongoDB区分类型和大小写(包括函数)
field名字不能有 “."/"null"(空格),不能以"$"开头
以下划线"_"开头的键是保留的(不是严格要求的)
文档的键是字符串
集合名不能以"system."开头,这是为系统集合保留的前缀

可视化管理工具

https://docs.mongodb.com/ecosystem/tools/administration-interfaces/
再linux上不要使用robomongo,数据量较大时没法完整读取
推荐使用mongochef:http://3t.io/mongochef/download/core/platform/#tab-id-3

b.system.* 包含多种系统信息的特殊集合(Collection)

集合命名空间 描述
db.system.namespaces 列出所有名字空间。
db.system.indexes 列出所有索引。
db.system.profile 包含数据库概要(profile)信息。
db.system.users 列出所有可访问数据库的用户。
db.local.sources 包含复制对端(slave)的服务器信息和状态

Import Example Dataset

获得数据例子
Import data into the collection

mongoimport --db test --collection restaurants --drop --file ~/downloads/primer-dataset.json

注意:
The mongoimport connects to a mongod instance running on localhost on port number 27017. The --file option provides the path to the data to import, in this case, ~/downloads/primer-dataset.json.

To import data into a mongod instance running on a different host or port, specify the hostname or port by including the --host and the --port options in your mongoimport comman

document

一些限制

如Maximun index key length

嵌入文档Embedded Documents

{
   ...
   name: { first: "Alan", last: "Turing" },
   contact: { phone: { type: "cell", number: "111-222-3333" } },
   ...
}

访问number:contact.phone.number

document field order

Starting in version 2.6, MongoDB actively attempts to preserve the field order in a document. Before version 2.6, MongoDB did not actively preserve the order of the fields in a document

_id field

The _id field has the following behavior and constraints:

  • By default, MongoDB creates a unique index on the _id field during the creation of a collection.
  • The _id field is always the first field in the documents. If the server receives a document that does not have the _id field first, then the server will move the field to the beginning.
  • The _id field may contain values of any BSON data type, other than an array.

Query Filter documents

specify the conditions that determine which records to select for read, update, and delete operations

{
  <field1>: <value1>,
  <field2>: { <operator>: <value> },
  ...
}

Update Specification Documents

{
  <operator1>: { <field1>: <value1>, ... },
  <operator2>: { <field2>: <value2>, ... },
  ...
}

Bson types

可以使用$type操作符
Type Number Alias Notes
Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
Boolean 8 “bool”
Date 9 “date”
Null 10 “null”
Regular Expression 11 “regex”
DBPointer 12 “dbPointer”
JavaScript 13 “javascript”
Symbol 14 “symbol”
JavaScript (with scope) 15 “javascriptWithScope”
32-bit integer 16 “int”
Timestamp 17 “timestamp”
64-bit integer 18 “long”
Min key -1 “minKey”
Max key 127 “maxKey”

ObjectId

consists of 12-bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation, specifically

a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.
原文地址:https://www.cnblogs.com/jcuan/p/5693621.html