Mongo DB
- About NoSQL
- About MongoDB
- Installing MONGO under the Windows platform
- Mongo DB Official documentation
MongoDB basic Commands
- The default installation path for MongoDB is C:\Program Files\mongodb
- Create the default database storage path c:\data\db, using the command line to associate the path to MONGO C:\Program Files\mongodb\server\3.2\bin\mongod.exe--dbpath c:\data\db, The database service opens and starts listening after successful execution
Mongo Shell
- Run the C:\Program files\mongodb\server\3.2\bin\mongod.exe file to open the MongoDB shell, which is a self-brought interactive JavaScript shell, An interactive environment for manipulating and managing MongoDB
- The help command can display a command line that can be used
DB-related operations
use tutorial
Use this command to attempt to connect to a database named tutorial and create if it does not exist. Use the Db.help () command to view command-line Help
show dbs
Displays information about the database.
If the database related name contains spaces and other characters, you can also use the following command
db["dbname"].find()db.getCollection("dbname").find()
Inserting data
Add data by using the following format:
db.restaurants.insert( { "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ], }, "borough" : "Manhattan", "cuisine" : "Italian", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Vella", "restaurant_id" : "41704620" })
Find data
If you want to find all the data, then:
db.collectionname.find()
Specify criteria:
Specify a field condition to filter by using the following format:
{ <field1>: <value1>, <field2>: <value2>, ... }
Specific examples are:
db.restaurants.find( { "borough": "Manhattan" } )
Greater than, less than the condition of the filter
db.restaurants.find( { "grades.score": { $gt: 30 } } )db.restaurants.find( { "grades.score": { $lt: 10 } } )
And And OR
db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )db.restaurants.find( { $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } )
Sort
db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )
Update data
The following action updates the record for name Juni and updates the Cuisine field with the $set action. To update the LastModified field with the $currentDate operator:
db.restaurants.update({ "name" : "Juni" },{ $set: { "cuisine": "American (New)" }, $currentDate: { "lastModified": true }})
To update the embedded data:
db.restaurants.update( { "restaurant_id" : "41156888" }, { $set: { "address.street": "East 31st Street" } })
Update multiple data: By default the Update method updates only one piece of data. To update more than one piece of data, use multi option.
db.restaurants.update( { "address.zipcode": "10016", cuisine: "Other" }, { $set: { cuisine: "Category To Be Determined" }, $currentDate: { "lastModified": true } }, { multi: true})
Replace a record with information from a _id field and replace it with a new record.
db.restaurants.update( { "restaurant_id" : "41704620" }, { "name" : "Vella 2", "address" : { "coord" : [ -73.9557413, 40.7720266 ], "building" : "1480", "street" : "2 Avenue", "zipcode" : "10075" } })
Delete a record
Delete all records that meet a certain condition:
db.restaurants.remove( { "borough": "Manhattan" } )
To delete only one record that meets a certain condition, use the Justone option:
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
To delete all records:
db.restaurants.remove( { } )
Delete a table:
db.restaurants.drop()
Aggregation operation aggregation and accumulation
Use $group to GROUP by a keyword, and in $group, specify the keyword to be grouped as _id. $group Access the field by field path, the field name needs to be prefixed with $. $sum represents an accumulator, the following statement represents the number of cases in which the Borough field is calculated.
db.restaurants.aggregate( [ { $group: { "_id": "$borough", "count": { $sum: 1 } } } ]);
The output is:
{ "_id" : "Staten Island", "count" : 969 }{ "_id" : "Brooklyn", "count" : 6086 }{ "_id" : "Manhattan", "count" : 10259 }{ "_id" : "Queens", "count" : 5656 }{ "_id" : "Bronx", "count" : 2338 }{ "_id" : "Missing", "count" : 51 }
Aggregating and filtering
Use $match to filter records
db.restaurants.aggregate( [ { $match: { "borough": "Queens", "cuisine": "Brazilian" } }, { $group: { "_id": "$address.zipcode" , "count": { $sum: 1 } } } ]);
Mongodb Learning Notes