免責聲明

Disclaimer (免責聲明)
繼續閱覽代表您接受以上的免責聲明.
To continue reading means you accept the above disclaimer.

2015年6月15日 星期一

mongo, mongodb client shell, query



//=== http://docs.mongodb.org/manual/tutorial/getting-started-with-the-mongo-shell/
$ mongo
> db
> use another_db
> db.collectionName.find();

//=== http://docs.mongodb.org/v2.2/reference/method/db.collection.find/

db.products.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 } )

This operation returns all the documents from the collection "products"
where "qty" is greater than 25.
The documents in the results y include the
"_id", "item", and "qty" fields

using “inclusion” projection by the parameter { item: 1, qty: 1 }

find() always returns the _id field, even when not explicitly included:

Results:
{ "_id" : 11, "item" : "pencil", "qty" : 50 }
{ "_id" : ObjectId("50634d86be4617f17bb159cd"), "item" : "bottle", "qty" : 30 }
{ "_id" : ObjectId("50634dbcbe4617f17bb159d0"), "item" : "paper", "qty" : 100}

"exlcude" projection
db.products.find( { qty: { $gt: 25 } }, { _id: 0, qty: 0 } )

Results:
{ "item" : "pencil", "type" : "no.2" }
{ "item" : "bottle", "type" : "blue" }
{ "item" : "paper" }



//=== http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/

*** mongo client shell
$ mongo
> db = connect("localhost:27017/db1");
> show collections;
> db.collect1.insert({k1:'v1',k2:'v2', k3:v3});

> db=connect("db2");
> show collections;

Differences Between Interactive and Scripted mongo ?


To set the db global variable, use the getDB() method or the connect() method. You can assign the database reference to a variable other than db.

You cannot use any shell helper (e.g. use , show dbs, etc.) inside the JavaScript file because they are not valid JavaScript.


hell Helpers
JavaScript Equivalents

show dbs, show databases
db.adminCommand('listDatabases')

use
db = db.getSiblingDB('')

show collections
db.getCollectionNames()

show users
db.getUsers()

show roles
db.getRoles({showBuiltinRoles: true})

show log
db.adminCommand({ 'getLog' : '' })

show logs
db.adminCommand({ 'getLog' : '*' })



it
cursor = db.collection.find()
if ( cursor.hasNext() ){
cursor.next();
}





沒有留言:

張貼留言