rs.add("<hostname>")replicaSetAdd("<hostname>")rs.insert("<hostname>")replica.add("<hostname>")db.citizens.select('WHERE age >= 21')db.citizens.where('age >= 21')db.citizens.find('WHERE age >= 21')db.citizens.find({age: {$gte: 21}})_id, how do you get the time it was created?getDateTime(_id)_id.createDate()_id.getTimestamp()_id.getDateTime()db.users.find({_id: 1})db.users.seek({_id: 1})db.users.query({_id: 1})db.query.users({_id: 1})--type jsonArray--json--type json--jsonArray--setParameter authenticationMechanisms=GSSAPI--setAuthentication=GSSAPI--setParam auth=K--setAuth method=Kerberosdb.product.group({_id: "$category", count: {$sum:1}})db.product.aggregate($sum: {_id: "$category", count: {$group:1}})db.product.aggregate($group: {_id: "$category", count: {$sum:1}})db.product.aggregate($count: {_id: "$category", count: {$group:1}})db.restaurants.createIndex({location: "2dsphere"})db.restaurants.geospatial({location: "2dsphere"})db.restaurants.createIndex("2dsphere":"location")db.restaurants.createIndex({geospatial: "location"})db.customers.findmatch ({"jobs":"secretary"})db.customers.find ({"jobs:secretary"})db.customers.find ({"jobs":["secretary"]})db.customers.find ({"jobs":"secretary"})db.customers.find({}, {skip: 5, limit: 10})db.customers.find({}.page(5).take(10))db.customers.find({}).skip(5).take(10)db.customers.find({}).skip(5).limit(10)db.customers.createIndex({firstName, lastName})db.customers.createTextIndex({firstName, lastName})db.customers.createIndex({firstName: "text", lastName: "text"})db.customers.createText({firstName: 1, lastName: 1})db.customers.createIndex("lastName, firstName, ASC")db.customers.addIndex({lastName:"ASC", firstName: "ASC"})db.customers.newIndex({lastName:1, firstName:1})db.customers.createIndex({lastName:1, firstName: 1})db.customers.all();db.find().customers();db.customers.find();db.customers.show();myCursor.stats()myCursor.dump()myCursor.info()myCursor.explain()_id fielddb.vehicle.stats(1024)db.vehicle.stats("kilobytes")db.vehicle.stats(true)db.vehicle.stats("kb")reIndex() command to modify the index.createIndex() command with the update option.updateIndex() command.db.vehicle.dropIndex("description_text")db.vehicle.dropIndex({"description":"text"})db.vehicle.removeIndex({"description":"text"})db.vehicle.removeIndex("description_text")db.vehicle.distinct("category")db.vehicle.unique("category")db.vehicle.distinct("category").count()db.vehicle.distinct("category").lengthNote: count() works with find(…) but length works with distinct
db.customers.add({name: "Bob"})db.customers.save({name: "Bob"})db.customers.create({name: "Bob"})db.customers.new({name: "Bob"})_id_namedb.persons.find().sort({lastName: -1}}db.persons.find().sort({lastName: 1}}db.persons.find().sort({lastName: ascending}}db.persons.find().sort({lastName: $asc}}db.customers.delete({_id: 1});db.customers.drop({_id: 1});db.drop.customers({_id: 1});db.customers.remove({_id: 1});db.customers.remove({}).indexes();db.customers.remove({});db.customers.drop();db.customers.delete();db.people.getName();db.people.reIndex({names: 1});db.people.getIndexKeys();db.people.getIndexes();db.members.aggregate([ {$match: {gender: "Female"}}, {$group: {_id: {city: "$city"}, number: {$sum: 1}}}, {$sort :{number: -1}}])db.members.find({$match: {gender: "Female"}}, {$group: {_id: {city: "$city"}, number: {$sum: 1}}}.$sort ({number: -1})db.members.find([ {$match: {gender: "Female"}}, {$group: {_id: {city: "$city"}, number: {$sum: 1}}}, {$sort :{number: -1}}])db.members.aggregate([ {$match: {gender: "Female"}}, {$sort :{number: -1}}])explain(), what mode does it run in?db.person.find({exists: 'homePhone'});db.person.exists({homePhone: true});db.person.find({homePhone: {$exists: true}});db.person.has('homePhone');mongod process.secure() command.mongoimport command.authenticate() command.dropDatabase()removeAll()clear()deleteDatabase()-db=null--shell-only--free-nodb_id?Use db.collection.set({$_id:pretty})Create a second indexUse db.collection.format(numeric)Use $_id = valueThe oplog will be saved on one of the secondary servers.The oplog is capped collection and can't run out of memoryThe MongoDB instance will failThe oplog will stop recording logging informationdb.performance.members.aggregate([ {$match: {gender: "Female"}}, {$group: {_id:{city:"$city"}, number: {$sum: 1}}}, {$sort : {number: -1}}])db.members.aggregate([ {$match: {gender: "Female"}}, {$group: {_id: {city: "$city"}, number:{$sum:1}}}, {$sort: {number:-1}}]).explain("executionStats")db.members.aggregate([ {$match: {gender: "Female"}}, {$group:{_id: {city: "$city"}, number: {$sum: 1}}}, {$sort: {number: -1}}]).explain()db.members.aggregate([ {$match: {gender: """Female"""}}, {$group: {_id: {city: """$city"""}, number: {$sum:1}}}, {$sort: {number: -1}}]).number()_idMongoDB documentation JSON and BSON
db.customers.find({}, {firstName: 1, lastName: 1})db.customers.find({}, {_id:0, firstName: 1, lastName: 1})db.customers.find({_id: 0, year: 1, maek: 1, model: 1})db.customers.find({}).project({firstName: 1, lastName: 1})db.inventory.insert({ prodid: "tab1122", qty : 10}, { writeConcern: { w: 2, wtimeout: 5000} })db.inventory.insert({ prodid: "tab1122", qty : 10}, { writeConcern: { j: true} })db.inventory.insert({ prodid: "tab1122", qty : 10}, { writeConcern: { w: 2, j:false, wtimeout: 5000} })db.inventory.insert({ prodid: "tab1122", qty : 10}, { writeConcern: { w: 2, j:true, wtimeout: 5000} })