added mongo and _mongo

pull/27/head
Igor Chubin 6 years ago
parent 7c499158b8
commit de9cd1b440

@ -0,0 +1,24 @@
// equals to
db.books.find({year: {$eq: 2016}})
// less than
db.books.find({year: {$lt: 2010}})
// less than or equal to
db.books.find({year: {$lte: 2008}})
// greater than
db.books.find({year: {$gt: 2014}})
// greater than or equal to
db.books.find({year: {$gte: 2008}})
// not equal to
db.books.find({year: {$ne: 2008}})
// value in
db.books.find({year: {$in: [2008, 2016]}})
// value not in
db.books.find({year: {$nin: [2008, 2016]}})

@ -0,0 +1,41 @@
// Insert a new document in a collection
db.books.insert({"isbn": 9780060859749, "title": "After Alice: A Novel", "author": "Gregory Maguire", "category": "Fiction", "year":2016})
// Insert multiple documents into a collection
db.books.insert([
{ "isbn":"9781853260001", "title": "Pride and Prejudice", "author": "Jane Austen", "category": "Fiction"},
{"isbn": "9780743273565", "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"}
])
// Show all documents in the collection
db.collection.find()
// Filter documents by field value condition
db.books.find({"title":"Treasure Island"})
// Show only some fields of matching documents
db.books.find({"title":"Treasure Island"}, {title:true, category:true, _id:false})
// Show the first document that matches the query condition
db.books.findOne({}, {_id:false})
// Update specific fields of a single document that match the query condition
db.books.update({title : "Treasure Island"}, {$set : {category :"Adventure Fiction"}})
// Remove certain fields of a single document the query condition:
// $unset
db.books.update({title : "Treasure Island"}, {$unset : {category:""}})
// Remove certain fields of all documents that match the query condition
// $unset, {multi:true}
db.books.update({category : "Fiction"}, {$unset : {category:""}}, {multi:true})
// Delete a single document that match the query condition
// {justOne:true}
db.books.remove({title :"Treasure Island"}, {justOne:true})
// Delete all documents matching a query condition
db.books.remove({"category" :"Fiction"})
// Delete all documents in a collection
db.books.remove({})

@ -0,0 +1,16 @@
// Show number of documents in the collection
db.books.find().count()
// Limit the number of documents to return
db.books.find().limit(2)
// Return the result set after skipping the first n number of documents
db.books.find().skip(2)
// Sort the documents in a result set in ascending or descending order of field values
db.books.find().sort( {title : 1} )
// value = 1 for ascending, -1 for descending
// Display formatted (more readable) result
db.books.find({}).pretty()

@ -0,0 +1,6 @@
// Match documents that contains that specified field
db.books.find( {category: {$exists: true }})
// Match documents whose field value is of the specified BSON data type
db.books.find( {category: {$type: 2 }})

@ -0,0 +1,19 @@
// Create an index
db.books.createIndex({title:1})
// Type 1 means ascending; -1 means descending
// Create a unique index
db.books.createIndex( {isbn:1},{unique:true} )
// Create a index on multiple fields (compound index)
db.books.createIndex({title:1, author:-1})
// Show all indexes in a collection
db.books.getIndexes()
// Drop an index
db.books.dropIndex({author:-1})
// Show index statistics
db.books.stats()

@ -0,0 +1,12 @@
// OR
db.books.find( { $or: [{year: {$lte: 2008}}, {year: {$eq: 2016}}]} )
// AND
db.books.find( { $and: [{year: {$eq: 2008}}, {category: {$eq: "Fiction"}}]} )
// NOT
db.books.find( {$not: {year: {$eq: 2016} }})
// NOR
db.books.find( { $nor: [{year: {$lte: 2008}}, {year: {$eq: 2016}}]} )

@ -0,0 +1,30 @@
// Show current database
db
// Select or switch database
use mydb
// Execute a JavaScript file
load(myscript.js)
// Display help
help
// Display help on DB methods
db.help()
// Display help on Collection
db.mycol.help()
// Show all databases
show dbs
// Show all collections in current database
show collections
// Show all users on current database
show users
// Show all roles on current database
show roles

@ -0,0 +1,25 @@
# Connect to local host on default port 27017
mongo
# Connect to remote host on specified port
mongo --host 10.121.65.23 --port 23020
# Connect to a database <host>/<database>
mongo 10.121.65.58/mydb
# Connect to a database on specified host and port
# with this username and password
mongo -u username -p password --port 12345 --host localhost
# Run the shell after executing my-script.js
mongo --shell my-script.js
# Connect to database on specified port and host
# and execute my-script.js after that
mongo localhost:27017/myDatabase my-script.js
# See also:
# MongoDB cheat sheets at /mongo/
# list of pages: /mongo/:list
# search in pages: /mongo/~keyword
Loading…
Cancel
Save