MongoDB Quick Start:
1 Download mongodb package and unzip to your target folder;
2 run "mongod --dbpath folderName" to start a mongodb instance; the -- dbpath specify the data folder path;3 run mongo to connect to the default local mongodb instance;
4 useful commands:
show dbs
show collections
use dbname
db.collectionName.find({criteria}, {projections}).limit(size).sort({modifier}).batchSize(size);
Note: the batchSize() doesn‘t change the output size(20) in the mongo shell;
How to exclude fields:
db.user.find({}, {‘fieldName1‘:0, ‘fieldName2‘: 0})
db.user.find({}, {‘_id‘:0})//exclude the id column;
$gt/$gte/$lt/$lte: the value of the column should great than/great than or equal/less than/less than or equal to the value specified;
db.people.find({score: {$gt: 80}}) will return people whose score great than 80;
$exists: return the documents that contains/don‘t contains the column specified;
db.people.find({hobbies: {$exists: true}});
$nin: return the documents that the filed value is on in the specified array, or the filed doesn‘t exist;
db.user.find({age: {$nin: [12, 24, 36, 48, 60, 72, 84, 96, 108]}})//选择非本命年的user
$ne: return the documents that the field value not equals the the value specified, or the field doesn‘t exist;
$type: column data type should match the type specified;
db.people.find({‘a‘: {$type: 1}}); will return the people whose attribute a should be double type;
The type list:
Double=1 String=2 Object=3 Array=4 (Binary data)=5
(Object id)=7 Boolean=8 Date=9 Null=10 (Regular expression)=11
(JavaScript code)=13 Symbol=14 (JavaScript code with scope)=15 (32-bit integer)=16 Timestamp=17 (64-bit integer)=18
$or: like ‘or‘ operation in SQL, return the documents match at least one criteria specified;
db.people.find({$or:[{score: 80},{score:90}]}) will return the people whose score is 80 or 90;
$and: like ‘and‘ operation in SQL, return the documents math all the criteria specified;
db.people.find({$and:[{score: {$gt:80}},{score:{$lt: 90}}]}) will return people whose score great than 80 but less than 90;
$all: The array column should contain all the elements specified;
db.people.find({favorites:{$all : [‘bear‘,‘cheese‘]}}) will return the people whose favorites should contain beer and cheese;
$in: the column should match at least one of the values specified;
db.people.find({name: {$in:[‘a‘,‘b‘]}}): will return people whose name is a or b;
query inside array: for array columns, if any value in the array match the criteria, then the document will be selected;
db.people.find({favorites: ‘beer‘}) will return any people who like to drink beer; // the favorites column is array;
query on nested documents: 如果需要匹配一个内嵌的文档,则内嵌文档必需完全匹配,比如你有:
{name: ‘xs06974‘,email: {work: ‘[email protected]‘, personal: ‘[email protected]‘}}
则下面的查询会返回记录:db.people.find({email: {work: ‘[email protected]‘, personal: ‘[email protected]‘}})但是下面的查询不会返回:db.people.find({email: {personal: ‘[email protected]‘,work: ‘[email protected]‘}}),尽管查询的email实质上应该是一致的;因为mongodb是按字节比较bson;
这个查询是可以的: db.people.find({‘email.work‘:‘[email protected]‘}); ‘email.work‘两边的引号是必需的;
Manipulate cursor:
var cur = db.people.find();
cur.hasNext(); will return if it contains any documents;cur.next() will return the next document;
cur.skip(n): will skip n documents;
cur.sort({colomnName: -1/1});
cur.limit(n) will only return n documents;
cursor的sort, limit, skip在server端被执行 一旦被执行,就不能再次调用;
count():
db.people.count(); count all the people;
db.people.count({score: {$gt: 80}}); count people whose score great than 80;
distinct():
db.user.distinct(‘age‘);
update():
db.collection.update({criteria}, {new document}); 该update方法将使用新的文档完全替代旧的文档;是比较危险的;
db.people.update({name: ‘john‘}, {name: ‘john1‘})
upsert:
db.people.update({name:‘xx06974‘},{$set:{age:30}}, {upsert: true}) update the documents if exist, otherwise, insert a new one;
$set:
change partial of the document;
db.people.update({‘name‘:‘john‘}, {$set: {age: 30}})
$unset:
remove some properties of a document;
db.people.update({‘name‘:‘john‘}, {$unset: {height: 1}})
Manipulate array:
$push:
db.people.update({name:‘john‘, {$push: {favorites: ‘bear‘}}});
$pushAll: db.people.update({name: ‘john‘}, {$pushAll:{a: [‘cheese‘,‘cocacola‘]}});
$pull:db.people.update({name: ‘john‘}, {$pull:{a: ‘cheese‘}});//remove from the array;$pullAll: db.people.update({name: ‘john‘}, {$pullAll:{a: [‘cheese‘,‘cocacola‘]}});// remove multiple from array;
$addToSet: db.people.update({name: ‘john‘}, {$pullAll:{a:‘cheese}});// add if not exists;
update multiple documents:db.people.update({}, {age: 12}, {multi: true}); will update all the documents matched; otherwise, will only update one document;
Please remember: Mongodb doesn‘t provide isolated transaction mechanism: there is a single thread to update the data, and it updates some documents for a operation A, then it will yield and let other operations to be executed; then it will pick up operation A again;
The mongodb can guarantee for a single document update;
User program can construct isolate mechanism in their program by intruducing lock;
remove();
db.people.remove({criteria}): remove matched documents one by one;
db.people.drop(); like truncate in SQL, remove the collections; the operation is in one transaction;
How to choose between refreences and embedded object(s) or How to choose between normalization and denormalization?
1 If two models will always be retrieved at the same time, it is better to use embedded object.
For example: the one-to-one relationship in User -> Contact; Otherwise, you should consider using reference;
2 If the embedded-object model leads to huge repetition, you should consider using reference:
For example: in the Book-Publisher relationship, some books may share the same publisher, so pub a embedded Publisher object under Book is not a good idea;
What should be considered in One-to-Many reference model?
We should consider in witch part should we store the reference; For example, in the Company-Employee relationship, if we store the reference into the company, the references size may grow as well as the company size grow;