2015年1月21日星期三

MongoDB week2 Notes

MongoDB’s CRUD operations exist as methods/functions in programming language APIs, not as a separated language.

db — current database
db.people.insert( ) — insert into collections
_id — the unique filed for all documents inserted into database, it is a primary key field, and it is immutable
The objectID is a global unique identifier, which is used for __id
db.people.findOne( ) — return randomly one document
db.people.findOne( ) || db.people.find( )
  • the first argument specific the criteria to match, like the WHERE clause
  • the second argument specific what field to return, like the SELECT clause
db.people.find( ) — find all documents in people collection
db.people.find( ).pretty( ) — change the format to show the result

db.people.find( { score : { $gt : 95 } } ) — query operator
db.people.find( { profession : { $exists : true } } ); — query on the structure of document
db.people.find( { name : { $type : 2 } } ); — query on the type of fields
db.people.find( { name : { $regex : “a” } } ); — regular expression matching on string
{ $or : [query1, query2, … , queryn] }
{ $and : [query1, query2, … , queryn] }
db.accounts.find( { favorites : “beer” } ) — query if an array contains the specific value, only check the top level and no recursion on the nested sub-documents.
db.accounts.find( { favorites : { $all : [ “beer”, “pretzels” ] } } ) — favorites contains all elements in the array, the order does not matter
db.accounts.find( { name : { $in : [ “xxx”, “yyy”] } } ) — the document which name is in the array, either xxx or yyy
db.users.find( { “email.work” : “xxxx” } ) — dot notation, allows to query for the embedded document

cursor.hasNext( ) — return true as long as there’s another document to visit on this cursor
cursor.next( ) — return next document to be visited
cursor.limit( 5 ) — limit the number of the document of the cursor, instruct the server to return specific number of document when cursor start to iterate
cursor.sort( { name : -1 } ) || cursor.skip( ) 
we could not modify the cursor once we have called hasNext( ) or next( ). limit, sort and skip are executed in server side not client side.
sort —> skip —> limit

db.scores.count( { xxx : yyy } ) — count the document
db.people.update( { name : “Smith” }, { name : “Tomas”, Salary : 50000 }) — the document which name is Smith would be replaced by the second argument which is a new document.
db.people.update( { name : “Smith” }, { $set : { name : “Tomas” } } ) — update the field only, if the field does not exist, it will be created
use $inc to increase the value of a specific field
db.people.update( { name : “Smith” }, { $unset : { professional : 1 } } ) — remove a field in a document
db.array.update( { xxx : yyy }, { $set : { “array.index” : zzz } } ) — use dot notation to specify the element in the array try to change
use $push to add an element into the array from the rightmost place
use $pop to remove the rightmost element int the array
use $pushAll to add append an array from the rightmost place
use $pull to remove an element from the array regardless of its position
use $pullAll to remove a list of element from the array
use $addToSet to treat the array as a set, if duplicates exist, it will do nothing
db.people.update( { }, { }, { upset : true } ) — insert a new document if the document does not exist
db.people.update( { }, { }, { multi : true} ) — update multiple documents
db.people.remove( { } ) — remove a document that matches the specific criteria

Nodejs

var MongoClient = require(‘mongodb’).MongoClient;
MongoClient.connect( ‘connect string here’, function(err db) { } )
db.collection( ‘collection name’ ).findOne( query, function(err, doc) { } )
db.collection( ‘collection name’ ).find( query ).toArray(function( err, docs ) { } )
var cursor = db.collection( ‘collection name’ ).find( query );
cursor.each( function( err, doc ) { } ) 
.find( ) will create a cursor object, only when the cursor call .each( ) or .toArray( ), it starts to retrieves data from database, the database will not return the entire result but a batch of the result
db.collection( ‘collection name’ ).find( query, projection )
cursor.sort( [ [ ‘grade’ , 1 ], [ ‘student’ , -1 ] ] ) —> use array in order to avoid the rearrange of the elements
db.collection( ‘collection name’ ).insert( doc, function( err, inserted ) { } )
db.collection( ‘collection name’ ).update( query, operator, options, function( err, updated ) { } )
we could not mix $operators with normal fields
db.collection( ‘collection name’ ).save( doc, function( err, saved ) { } ) — check to see if the doc exist (_id), if not, then a new document would be inserted otherwise, replacement would be done
findAndModify( query, sort, operator, option, callback ) — atomically find and returns the document, no two client would conflict here on the document

Java 

The parameter of all method is DBObject, which is used to represent a document. — BasicDBObject
MongoClient client = new MongoClient( )
DB courseDB = client.getDB(“xxx”)


DBCollection collection = courseDB.getCollection(“xxx”)

没有评论:

发表评论