runCommand() – Run one or more specified database commands

Helps to run specified database commands commands.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.runCommand(<command>)

Command parameters

command
A database command, specified either in document form or as a string.
The following command parameters can be used:
convertToCapped
Converts a collection into a capped collection.
Syntax:
 db.runCommand({convertToCapped: <collection>, size: <capped size> }) 
insert
Inserts one or more documents into a collection.
Syntax:
db.runCommand({insert: <collection>, documents:[json] })
delete
Deletes one or more documents in a collection.
Syntax:
db.runCommand({delete: <collection>, deletes:{query, limit} }) 

where limit limits the matching document to delete. Enter 0 to delete all matching documents.

update
Updates one or more documents in a collection.
Syntax:
db.runCommand({update: <collection>, updates:{query, updateSpec, multi, upsert} }) 
where
  • multi , when set to true; updates all the documents that meet the query criteria. The default value is false.
  • upsert, when set to true, runs an insert operation if no document matches the query. The default value is false.
find
Find all documents or a select set of documents from a collection.
Syntax:
db.runCommand({find: <collection>,query, limit, skip} }) 
where
  • query is a JSON query for filtering rows.
  • skip skips the number of specified rows.
  • limit specifies the maximum number of rows to return from the collection
findAndModify
Find the first document in a query and update the document.
Syntax:
 db.runCommand({findAndModify: <collection>,query,fields,sort,remove,updateSpec,returnNew,upsert,fullresponce} }) 
where
  • query is an optional filter for selecting a subset of documents.
  • Fields is an optional parameter for selecting fields to include or exclude. Enter 0 to exclude or 1 to include
  • sort is an optional JSON object for specifying sort criteria.
  • removeis an optional parameter for removing or updating documents. Enter true to remove matching documents or false to update matching documents. The default is false.
  • updateSpec is a JSON object containing the update to apply
  • returnNew is an optional parameter for returning an updated document. Enter true to return the updated document or enter false to return the old version of the document. The default is false. )
  • upsert is an optional parameter for updating a document if the document is present in the collection. Enter true to update the document or enter false to not update the document. The default is false.
  • fullresponse is an optional parameter for returning a lastError object and document. Enter true to return the lastError object and document or enter false to return only the document. The default is false.
count
Returns the number of documents in a collection.
Syntax:
db.runCommand({count: <collection>,query} })
where query is an optional filter for selecting a subset of documents.

Examples

The following example shows the command syntax for converting the collection, mycoll, to a capped collection:
db.runCommand({
	“convertToCapped” : “mycoll”,
	size : 100000
})
The following example shows the command syntax for inserting documents into the collection, books:
db.runCommand({
	“insert” : “books”,
	“documents” : [ 
		{ “isbn” : “123-456-789”, “author” : “Verne, Jules”,
		“title” : “Journey to the Center of the Earth”
		}
	]
})
The following example shows the command syntax for deleting documents from the collection, mycoll:
db.runCommand({
	“delete” : “mycoll”,
	“deletes” : {
		“query” : { “points” : { $gt : 2500 } },
		“limit” : 4
	}
})
The following example shows the command syntax for updating documents in the collection, mycoll:
db.runCommand({
	“update” : “mycoll”,
	“updates” : {
		“query” : { “points” : { $gt : 2500 } },
		“updateSpec : { $set : { “assignment” : 5 } },
		“multi” : true,
		“upsert” : false
	}
})
The following example shows the command syntax for finding the documents points and assignment in the collection, mycoll:
db.runCommand({
	“find” : “mycoll”,
	“query” : { 
		“points” : { $gt : 2500 },
		“assignment” : { $lt : 50 }
	},
	“limit” : 3,
	“skip” : 1
})

db.runCommand({
	“find” : “mycoll”,
})
The following example shows the command syntax for finding and updating the document points in the collection, mycoll:
db.runCommand({
	“findAndModify” : “mycoll”,
	“query” : { “points” : 1 },
	“updateSpec” : { $set : { “status” : “C” } },
	“returnNew” : true
})

db.runCommand({
	“findAndModify” : “mycoll”,
	“query” : { “points” : 0 },
	“remove” : true
})
The following example shows the command syntax for finding the number of documents in the collection, mycoll:
db.runCommand({
	“count” : “mycoll”
})

db.runCommand({
	“count” : “mycoll”
	“query” : { “points” : { $gt : 17 } }
})