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()
myCursor.hasNext()
myCursor.sort()
myCursor.next()
myCursor.find()
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
db.customers.find({lastName: 'smith'}).explain()
db.customers.find({lastName: 'smith'}).perf()
db.customers.find({lastName: 'smith'}).plan()
db.customers.find({lastName: 'smith'}).usedIndex()
db.deleteUser("user")
db.removeUser("user") DEPRECATED
db.remove("user")
db.dropUser("user")
--setParameter authenticationMechanisms=GSSAPI
--setAuthentication=GSSAPI
--setParam auth=K
--setAuth method=Kerberos
db.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").length
Note: 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
_name
db.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});
Note: db.collection.remove() is deprecated in the new mongosh. Use db.collection.deleteOne() or db.collection.deleteMany().
References: db.collection.remove() db.collection.delete()
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();
Note: An alternative method in the mongosh shell is listIndexes()
Note: An alternative method for db is .update()
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}}])
Note: If you want to analyze the performance of a query use .explain(“executionStats”)
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.Note: You don’t need an index to perform ad hoc queries. Only pick one choice Reference
dropDatabase()
removeAll()
clear()
deleteDatabase()
-db=null
--shell-only
--free
-nodb
_id
?Use db.collection.set({$_id:pretty})
Use db.collection.find().pretty()
Use db.collection.format(numeric)
Use $_id = value
The oplog will be saved on one of the secondary servers.
The oplog is capped collection and can't run out of memory
The MongoDB instance will fail
The oplog will stop recording logging information
Argument:
Why “The oplog will be saved on one of the secondary servers.” is wrong:
MongoDB applies database operations on the primary and then records the operations on the primary’s oplog. The secondary members then copy and apply these operations in an asynchronous process. All replica set members contain a copy of the oplog, in the local.oplog.rs collection, which allows them to maintain the current state of the database.
Reasoning behind the right answer:
The oplog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases.
Unlike other capped collections, the oplog can grow past its configured size limit to avoid deleting the majority commit point.
Note: mongosh is the new mongo shell, mongo is deprecated.
Starting in MongoDB v5.0, mongosh replaces mongo as the preferred shell.
[Reference:] (https://www.mongodb.com/docs/mongodb-shell/)
db.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()
node 'list.js'
exec('list.js)
run('list.js)
load('list.js)
db.customers.sort({name: -1}.find({})
db.customers.sort({name: -1})
db.customers.find({}).sort({name: -1})
db.customers.find({}).sort({name: 1})
mongoimport
command to import personnel data and there is a unique index on the email field. What happens when there are duplicate emails in the import?Note: By default, mongoimport continues an operation when it encounters duplicate key and document validation errors.
Argument: You need a text index in order to perform a $text query on a field. $text query uses the text index under the hood
References: $text query Text index
_id
Note: The question in case is ambiguous. In the mongo docs, on the Windows Installation section, it clearly specifies the need for creating a data directory. However, that does not seem to be the case for Unix based systems. So, that gives use the closest possible solution for a specific platform. If we extrapolate that to the question, that seems to be the most reasonable solution.
db.size()
db.info()
db.memory()
db.stats()
db.createUser({})
db.insert({user: 1})
db.customers.newUser({})
db.newUser({})
MongoDB 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})
Concurrency Changed in version 4.2.
MongoDB uses an optimized build process that obtains and holds an exclusive lock on the specified collection at the start and end of the index build. All subsequent operations on the collection must wait until createIndex() releases the exclusive lock. createIndex() allows interleaving read and write operations during the majority of the index build.
For more information on the locking behavior of createIndex(), see Index Builds on Populated Collections.
Argument: There is no -d option in the docs (https://www.mongodb.com/docs/database-tools/mongoimport/#options.)
Note: Assuming you are asked to drop a collection instead while importing, the use the --drop option.
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} })
db.customers.find({lastName: 'smith'}).explain()
db.customers.find({lastName: 'smith'}).perf()
db.customers.find({lastName: 'smith'}).plan()
db.customers.find({lastName: 'smith'}).usedIndex()
db.restaurants.createIndex({location: "2dsphere"})
db.restaurants.geospatial({location: "2dsphere"})
db.restaurants.createIndex("2dsphere":"location")
db.restaurants.createIndex({geospatial: "location"})
The oplog will be saved on one of the secondary servers.
The oplog is capped collection and can't run out of memory
The MongoDB instance will fail
The oplog will stop recording logging information
db.customers.remove({}).indexes();
db.customers.remove({});
db.customers.drop();
db.customers.delete();
Explanation : MongoDB’s primary advantage as a NoSQL database over traditional relational databases is its schema flexibility.
createDatabase()
useDatabase()
db.createDatabase()
use
$eq
$ne
$gt
$lt
mongod
process in MongoDB?deleteOne()
removeOne()
drop()
remove()
$group
$match
$project
$sort
update()
modify()
change()
set()
limit()
count()
max()
skip()
$or
$and
$not
$nor
addIndex()
index()
createIndex()
makeIndex()
mongos
process in MongoDB?