Common MongoDB commands – Penetration Testing

MongoDB is a distributed database based on the database, written by C + +, between the relational database and non-relational database between the products, so in many business can replace mysql, to provide higher performance and better scalability. Although MongoDB does not support sql syntax, but from the commonly used command and sql usage is similar.

What are the databases for the query?

show dbs

Switch the database


If the database does not exist, create the database, otherwise switch to the specified database. But only in the database to insert data, will be displayed in the query database list.

Queries all the collections of the current database


View the status information for the specified collection


“ns” : “.”,
“count” : 1,
“size” : 4080,
“avgObjSize” : 4080,
“numExtents” : 1,
“storageSize” : 8192,
“lastExtentSize” : 8192,
“paddingFactor” : 1,
“paddingFactorNote” : “paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.”,
“userFlags” : 1,
“capped” : false,
“nindexes” : 1,
“totalIndexSize” : 8176,
“indexSizes” : {
“_id_” : 8176
“ok” : 1

View all the indexes in the current database


View the index in the specified collection


Creates an index in the specified collection

db.col.ensureIndex({“name”: 1, “age”: -1})

Index by name and age, 1 for ascending order, and -1 for descending order.

View the documents in the collection

Query all documents


Display the contents of the document in an easy-to-read manner


Query the top 10 documents


Sort the results of the query


The query results are sorted by clock, 1 is in ascending order, -1 is in descending order.

Where condition



Similar statements in RDBMS


Db.col.find ({“key”: “value”}). Pretty ()

Where key = ‘value’

Less than

Db.col.find ({“key”: {$ lt; 50}}). Pretty ()

Where key <50

Less than or equal to

Db.col.find ({“key”: {$ lte: 50}}). Pretty ()

Where key 50

greater than or equal to

Db.col.find ({“key”: {$ gte: 50}}). Pretty ()

Where key> = 50

not equal to

Db.col.find ({“key”: {$ ne: 50}}). Pretty ()

Where key! = 50

And condition

The find() method can pass multiple keys, each separated by commas.

db.col.find({key1:value1, key2:value2}).pretty()

Or condition

$or: [
{key1: value1}, {key2:value2}

E.g db.col.find({$or:[{“key1″:”value1”}, {“key2”: “value2”}]}).pretty()

Update operation

db.collection.update( query, update, upsert, multi )

Query : update query conditions, similar to sql update operation where clause.

Update : update the object and some of the updated operators (such as $set, $inc …), etc., can also be understood as sql update operation set clause.

Upsert : optional, this parameter means that if there is no record of the update, whether to insert a new record, true for the insert, the default is false, do not insert.

Multi : optional, the default is false, only to find the first record found, if this parameter is true, put the conditions to check out all the records of all updates.

db.agent_conf.update({hostid:137}, {$set: {upload:{interval:10}}}, true)

The path of the hostid = 137 in the agent_conf collection changes the value of upload.interval to 3, and if it does not exist, it only modifies the first entry of the match.

Delete operation


The post Common MongoDB commands appeared first on Penetration Testing.


Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )


Conectando a %s