Found 1659 Articles for Big Data Analytics

Remove only one document in MongoDB?

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

92 Views

To remove only a single document, use the remove() in MongoDB. Let us first create a collection with documents −> db.removeOnlyOneDocumentDemo.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6ca2f9cb58ca2b005e674") } > db.removeOnlyOneDocumentDemo.insertOne({"FirstName":"Carol", "LastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6ca399cb58ca2b005e675") } > db.removeOnlyOneDocumentDemo.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6ca429cb58ca2b005e676") }Following is the query to display all documents from a collection with the help of find() method −> db.removeOnlyOneDocumentDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6ca2f9cb58ca2b005e674"),    "FirstName" : "John",    "LastName" : "Smith" } {    "_id" : ObjectId("5cc6ca399cb58ca2b005e675"), ... Read More

How to remove key fields in MongoDB?

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

1K+ Views

To remove key fields in MongoFB, you can use $unset operator. Let us first create a collection with documents −>db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6c8289cb58ca2b005e672") } >db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6c8359cb58ca2b005e673") }Following is the query to display all documents from a collection with the help of find() method −> db.removeKeyFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6c8289cb58ca2b005e672"),    "StudentFirstName" : "John",    "StudentLastName" : "Doe",    "StudentAge" : 23 } {    "_id" : ObjectId("5cc6c8359cb58ca2b005e673"),    "StudentFirstName" : "John",    "StudentLastName" : "Smith",   ... Read More

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

Advertisements