MongoDB 学习笔记

(the little MongoDB book笔记)

Install

3. Create a new text file in the bin subfolder named mongodb.config

4. Add a single line to your mongodb.config: dbpath=PATH_TO_WHERE_YOU_WANT_TO_STORE_YOUR_DATABASE_

5. Make sure the dbpath you specified exists

6. Launch mongod with the --config /path/to/your/mongodb.config parameter.

You can now launch mongo (without the d) which will connect a shell to your running server.
Try entering db.version() to make sure everything's working as it should. Hopefully you'll
see the version number you installed.

Chapter 1 - The Basics

可以认为Collection里存的document就是HTML(文档),标记即field,每个标记的内容为field的value,Collection没有Schema.只要是文档就能往Collection里存

when you ask MongoDB for data, it returns a cursor,which we can do things to, such as counting or skipping ahead, without actually pulling down data.

There are some global commands you can execute, like help or exit.

 Commands that you execute against the current database
are executed against the db object, such as db.help() or db.stats() .

Because this is a JavaScript shell, if you execute a method and omit the parentheses (), you'll see the method body rather than executing the method. 

system.indexes

db.unicorns.insert({name: 'Leto', gender: 'm', home: 'Arrakeen', worm: false})
is created once per database and contains the information on our databases index.

By default, the _id field is indexed - which explains why the system.indexes collection was
created. You can look at system.indexes:
db.system.indexes.find()

Mastering Selectors

As such, you use it
when finding, counting, updating and removing documents from collections. A selector is a
JSON object , the simplest of which is {} which matches all documents (null works too). If
we wanted to find all female unicorns, we could use {gender:'f'}.

First, remove
what we've put so far in the unicorns collection via: db.unicorns.remove() (since we aren't
supplying a selector, it'll remove all documents). (null)

The special $lt, $lte, $gt, $gte and $ne 

The ObjectId which MongoDB generated for our _id field can be selected like so:
9
db.unicorns.find({_id: ObjectId("TheObjectId")})

The special $lt, $lte, $gt, $gte and $ne are used for less than, less than or
equal, greater than, greater than or equal and not equal operations. For example, to get all
male unicorns that weigh more than 700 pounds, we could do:
db.unicorns.find({gender: 'm', weight: {$gt: 700}})
//or (not quite the same thing, but for demonstration purposes)
db.unicorns.find({gender: {$ne: 'f'}, weight: {$gte: 701}})
The $exists operator is used for matching the presence or absence of a field, for example:
db.unicorns.find({vampires: {$exists: false}})
Should return a single document. If we want to OR rather than AND we use the $or operator
and assign it to an array of values we want or'd:
db.unicorns.find({gender: 'f', $or: [{loves: 'apple'}, {loves: 'orange'}, {
weight: {$lt: 500}}]})
The above will return all female unicorns which either love apples or oranges or weigh less
than 500 pounds.
There's something pretty neat going on in our last example. You might have already noticed,
but the loves field is an array. MongoDB supports arrays as first class objects. This is an
incredibly handy feature. Once you start using it, you wonder how you ever lived without
it. What's more interesting is how easy selecting based on an array value is: {loves: '
watermelon'} will return any document where watermelon is a value of loves.?ObjectId是什么,不都是数组对象。数组对象相当与一个Key多个Values.

The _id value can be of any type with type ObjectId being the most common. _id must be unique for each document in a collection. In most cases collections automatically have an _id index which includes a unique key constraint that enforces this.

If a user tries to insert a document without providing an _id field, the database will automatically generate an _object id_ and store it the _id field.

The _id value may be of any type, other than arrays, so long as it is a unique. If your document has a natural primary key that is immutable we recommend you use that in _id instead of the automatically generated ids. 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值