Found 1349 Articles for MongoDB

Retrieve the position in an Array in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

250 Views

You can use the concept of map reduce to get the position in an array. Let us first create a collection with documents −> db.retrievePositionDemo.find(); { "_id" : ObjectId("5cd569ec7924bb85b3f4893f"), "Subjects" : [ "MySQL", "MongoDB", "Java" ] }Following is the query to display all documents from a collection with the help of find() method −> db.retrievePositionDemo.find().pretty();This will produce the following output −{     "_id" : ObjectId("5cd569ec7924bb85b3f4893f"),     "Subjects" : [        "MySQL",        "MongoDB",         "Java"     ] }Following is the query to retrieve the position in an array in MongoDB ... Read More

MongoDB multidimensional array projection?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

524 Views

For MongoDB multidimensional array projection, you need to use aggregate framework. Let us first create a collection with documents. Here, we have multidimensional array for Student marks −> db.multiDimensionalArrayProjection.insertOne( ...    { ...       "StudentFirstName" : "Chris", ...       "StudentMarks" : [ [98, 99], [56, 79] ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b75a9cb58ca2b005e66c") }Following is the query to display all documents from a collection with the help of find() method −> db.multiDimensionalArrayProjection.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6b75a9cb58ca2b005e66c"),    "StudentFirstName" : "Chris",    "StudentMarks" ... Read More

How to maintain the top count of array elements in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

92 Views

You can use aggregate framework. Let us first create a collection with documents −> db.topCountArrayDemo.insertOne( ...    {"StudentId":101 , "StudentSubject": ["C", "MongoDB"]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b3209cb58ca2b005e669") } > db.topCountArrayDemo.insertOne( ...    {"StudentId":102 , "StudentSubject": ["C", "Java"]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b3219cb58ca2b005e66a") } > db.topCountArrayDemo.insertOne( ...    {"StudentId":103 , "StudentSubject": ["C", "MongoDB"]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b3229cb58ca2b005e66b") }Following is the query to display all documents from a collection with the help of find() method −> db.topCountArrayDemo.find().pretty();This will produce the following output −{ ... Read More

How to create a user in MongoDB v3?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

130 Views

To create a user in MongoDB v3, use the createUser() method. This allows you to create a user and while creating you need to add user, password, and roles as well. These roles assign permissions. The syntax is as follows −use admin db.createUser(    {       user: “yourUserName",       pwd: "yourPassword",       roles: [ { role: "yourPermission", db: "yourDatabase" } ]    } );Let us implement the above syntax in order to create a user In MongoDB v3 −> use admin switched to db admin > db.createUser( ...    { ...       ... Read More

Select two fields and return a sorted array with their distinct values in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

360 Views

To select two fields and return a sorted array with distinct values, use aggregate framework along with $setUnion operator. Let us first create a collection with documents −> db.sortedArrayWithDistinctDemo.insertOne( ...    { value1: 4, value2: 5} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e666") } > db.sortedArrayWithDistinctDemo.insertOne( ...    {value1: 5, value2: 6} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e667") } > db.sortedArrayWithDistinctDemo.insertOne( ...    {value1: 7, value2: 4} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e668") }Following is the query to display all documents from a collection with the ... Read More

How do I check whether a field contains null value in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

281 Views

You can use $type operator to check whether a filed contains null value. Let us first create a collection with documents. We have also inserted a null in a field −> db.nullDemo.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc68a1eac184d684e3fa270") } > db.nullDemo.insertOne({"FirstName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc68a25ac184d684e3fa271") } > db.nullDemo.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc68a2cac184d684e3fa272") }Following is the query to display all documents from a collection with the help of find() method −> db.nullDemo.find().pretty();This will produce the following output. One of the field is null −{ "_id" : ObjectId("5cc68a1eac184d684e3fa270"), "FirstName" ... Read More

Use result from MongoDB in shell script?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

465 Views

Let us first create a collection with a document −>db.useResultDemo.insertOne({"StudentFirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda70f5b50a6c6dd317adcd") }Display all documents from a collection with the help of find() method −> db.useResultDemo.find();Following is the output −{ "_id" : ObjectId("5cda70f5b50a6c6dd317adcd"), "StudentFirstName" : "Robert" }Here is the query to use result from MongoDB in shell script with var keyword −> var studentName=db.useResultDemo.findOne({},{_id:0}); > studentNameFollowing is the output −{ "StudentFirstName" : "Robert" }

How to select objects where an array contains only a specific field in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

240 Views

Let us first create a collection with documents −> db.arrayContainOnlySpecificFieldDemo.insertOne( ...    { ...       "StudentName":"John", ...       "StudentAge":21, ...       "StudentTechnicalSubject":["C", "Java", "MongoDB"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc4921dac184d684e3fa26a") } > db.arrayContainOnlySpecificFieldDemo.insertOne( { "StudentName":"Carol", "StudentAge":23, "StudentTechnicalSubject":["MongoDB"] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc49237ac184d684e3fa26b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayContainOnlySpecificFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc4921dac184d684e3fa26a"),    "StudentName" : "John",    "StudentAge" : 21,   ... Read More

Get attribute list from MongoDB object?

Samual Sam
Updated on 30-Jul-2019 22:30:26

923 Views

To get attribute list from MongoDB object, you can use for loop to extract key and value for document. Let us create a collection with documents −>db.getAttributeListDemo.insertOne({"StudentId":101, "StudentName":"John", "StudentAdmissi onDate":new ISODate('2019-01-12'), "StudentSUbjects":["MongoDB", "Java", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdfcc9ac184d684e3fa269") }Display all documents from a collection with the help of find() method −> db.getAttributeListDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbdfcc9ac184d684e3fa269"),    "StudentId" : 101,    "StudentName" : "John",    "StudentAdmissionDate" : ISODate("2019-01-12T00:00:00Z"),    "StudentSUbjects" : [       "MongoDB",       "Java",       "MySQL"    ] }Following is the ... Read More

Check for Existing Document in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

87 Views

You can use findOne() for this. Following is the syntax −db.yourCollectionName.findOne({yourFieldName: 'yourValue'});Let us create a collection with documents −> db.checkExistingDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf90dac184d684e3fa265") } > db.checkExistingDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf912ac184d684e3fa266") } > db.checkExistingDemo.insertOne({"StudentName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf916ac184d684e3fa267") } > db.checkExistingDemo.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf91bac184d684e3fa268") }Display all documents from a collection with the help of find() method −> db.checkExistingDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdf90dac184d684e3fa265"), "StudentName" : "John" } { "_id" : ObjectId("5cbdf912ac184d684e3fa266"), "StudentName" : "Carol" } ... Read More

Advertisements