Found 1349 Articles for MongoDB

Renaming column name in a MongoDB collection?

George John
Updated on 30-Jul-2019 22:30:25

567 Views

To rename column name in a collection, you can use $rename operator. Following is the syntaxdb.yourCollectionName.update({}, {$rename: {'yourOldColumName': 'yourNewColumnName'}}, false, true);Let us first create a collection with documents:> db.renamingColumnNameDemo.insertOne({"StudentName":"Larry", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ee2c6d628fa4220163b9a") } > db.renamingColumnNameDemo.insertOne({"StudentName":"Sam", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ee2d0d628fa4220163b9b") } > db.renamingColumnNameDemo.insertOne({"StudentName":"Robert", "Age":27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ee2dbd628fa4220163b9c") }Following is the query to display all documents from a collection with the help of find() method> db.renamingColumnNameDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9ee2c6d628fa4220163b9a"),    "StudentName" : "Larry",    "Age" : 23 ... Read More

Get the average of an entire field using the aggregation framework in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

80 Views

You can use aggregate() method for this. Let us first create a collection with documents> db.averageAggregationDemo.insertOne({"PlayerGameScore":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed66bd628fa4220163b95") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed671d628fa4220163b96") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed676d628fa4220163b97") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed67bd628fa4220163b98") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":16}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed701d628fa4220163b99") }Following is the query to display all documents from a collection with the help of find() method> db.averageAggregationDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9ed66bd628fa4220163b95"), "PlayerGameScore" ... Read More

How to store query result (a single document) into a variable?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

453 Views

To store query result (a single document) into a variable, you can use var. Following is the syntaxvar anyVariableName=db.yourCollectionName.find().limit(1); yourVariableName; //Print the records;Let us first create a collection with documents> db.storeQueryResultDemo.insertOne({"ClientName":"Chris", "ClientAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecf8fd628fa4220163b8d") } > db.storeQueryResultDemo.insertOne({"ClientName":"Robert", "ClientAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecf97d628fa4220163b8e") } > db.storeQueryResultDemo.insertOne({"ClientName":"Sam", "ClientAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecf9ed628fa4220163b8f") } > db.storeQueryResultDemo.insertOne({"ClientName":"Mike", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecfa8d628fa4220163b90") }Following is the query to display all documents from a collection with the help of find() method> db.storeQueryResultDemo.find().pretty();This will produce ... Read More

How to find two random documents in a MongoDB collection of 6?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

134 Views

Let us first create a collection and add some documents to it> db.twoRandomDocumentDemo.insertOne({"StudentId":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9aad628fa4220163b87") } > db.twoRandomDocumentDemo.insertOne({"StudentId":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9add628fa4220163b88") } > db.twoRandomDocumentDemo.insertOne({"StudentId":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9b0d628fa4220163b89") } > db.twoRandomDocumentDemo.insertOne({"StudentId":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9b3d628fa4220163b8a") } > db.twoRandomDocumentDemo.insertOne({"StudentId":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9b7d628fa4220163b8b") } > db.twoRandomDocumentDemo.insertOne({"StudentId":7}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9bad628fa4220163b8c") }Following is the query to display all documents from a collection with the help of find() method> db.twoRandomDocumentDemo.find();This ... Read More

How to check if field is a number in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

413 Views

To check if field is a number in MongoDB, use the $type operator. Following is the syntaxdb.yourCollectionName.find({youtFieldName: {$type:"number"}}).pretty();Let us first create a collection with documents> db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"John", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec75dd628fa4220163b83") } >db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"Chris", "StudentMathScore":98, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec77cd628fa4220163b84") } >db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"Robert", "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec7a4d628fa4220163b85") } >db.checkIfFieldIsNumberDemo.insertOne({"StudentId":101, "StudentName":"Larry", "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec7ccd628fa4220163b86") }Following is the query to display all documents from a collection with the help of find() method> db.checkIfFieldIsNumberDemo.find().pretty();This will produce the following output{    "_id" : ... Read More

Building Multiple Indexes at once in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

1K+ Views

In order to build multiple indexes at once, you need to use createIndexes() and pass multiple keys into an array. Following is the query for building multiple indexes at once.>db.multipleIndexesDemo.createIndexes([{"First":1}, {"Second":1}, {"Third":1}, {"Fourth":1}, {"Fifth":1}]);This will produce the following output{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 6,    "ok" : 1 }Now get all the indexes> db.multipleIndexesDemo.getIndexes();This will produce the following output[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_",       ... Read More

How to get documents by tags in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

441 Views

You can use $elemMatch operator for this. Let us create a collection with documents> db.getDocumentsByTagsDemo.insertOne({"Tags":["Tag-1", "Tag-2", "Tag-3"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9eb4d5d628fa4220163b79") } > db.getDocumentsByTagsDemo.insertOne({"Tags":["Tag-2", "Tag-4", "Tag-5"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9eb4d5d628fa4220163b7a") } > db.getDocumentsByTagsDemo.insertOne({"Tags":["Tag-6", "Tag-4", "Tag-3"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9eb4d6d628fa4220163b7b") }Following is the query to display all documents from a collection with the help of find() method> db.getDocumentsByTagsDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9eb4d5d628fa4220163b79"),    "Tags" : [       "Tag-1",       "Tag-2",       "Tag-3"    ] } ... Read More

How to determine whether a field exists in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

110 Views

You need to use $exists operator to determine whether a field exists in MongoDB. Let us first create a collection with documents> db.determineFieldExistsDemo.insertOne({"ClientName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9eb245d628fa4220163b75") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Larry", "ClientAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9eb25cd628fa4220163b76") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Mike", "ClientCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9eb26fd628fa4220163b77") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Sam", "ClientAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9eb286d628fa4220163b78") }Following is the query to display all documents from a collection with the help of find() method> db.determineFieldExistsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9eb245d628fa4220163b75"), "ClientName" : "John" ... Read More

How to get connected clients in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

599 Views

To get connected clients in MongoDB, use currentOp() with the set value to true and you need to iterate array result set with the help of field client. Let us first implement currentOp> db.currentOp(true)Following is the output. Here the client is 127.0.0.1 since we are using localhost. The output displays all the connected clients{    "inprog" : [       {          "host" : "DESKTOP-QN2RB3H:27017",          "desc" : "conn1",          "connectionId" : 1,          "client" : "127.0.0.1:61787",          "appName" : "MongoDB Shell",     ... Read More

How do you limit an array sub-element in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

660 Views

You can use $slice operator to limit an array. Let us create a collection with documents. Following is the query> db.limitAnArrayDemo.insertOne( ...    { ...       _id: 101, ...       "PlayerName": "Bob", ...       "PlayerDetails": {Age:23, isStudent:true}, ...       "PlayerGameScores": [234, 5767, 58, 67, 78, 90, 1002, 576, 68, 45, 23, 45, 678, 89, 78 ] ...    } ... ); { "acknowledged" : true, "insertedId" : 101 }Following is the query to display all documents from a collection with the help of find() method> db.limitAnArrayDemo.find().pretty();This will produce the following output{   ... Read More

Advertisements