Found 1659 Articles for Big Data Analytics

Search a string with special characters in a MongoDB document?

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

2K+ Views

To search a string with special characters in MongoDB document, you can use \. Here, we have special character $ in our string.Let us first implement the following query to create a collection with documents>db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Smith$John123", "UserFirstName":"John", "UserLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987b98330fd0aa0d2fe4b1") } >db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Taylor$Carol983", "UserFirstName":"Carol", "UserLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987bdb330fd0aa0d2fe4b2") } >db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Doe$John999", "UserFirstName":"John", "UserLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987bee330fd0aa0d2fe4b3") } >db.searchDocumentWithSpecialCharactersDemo.insertOne({"UserId":"Miller$David555", "UserFirstName":"David", "UserLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987c01330fd0aa0d2fe4b4") }Following is the query to display all documents from a collection with the help of ... Read More

Can we use NOT and AND together in MongoDB?

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

161 Views

Yes, we can use the NOT and AND together in MongoDB. The syntax is as followsNOT X AND NOT Y = NOT (X AND Y) Let us see the working of above syntax. If both X and Y will be true then last result will be false. If one of the operands gives result false then last result will be true.Following is the query to create a collection with documents> db.NotAndDemo.insertOne({"StudentName":"John", "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98746a330fd0aa0d2fe4a8") } > db.NotAndDemo.insertOne({"StudentName":"John", "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c987478330fd0aa0d2fe4a9") } > db.NotAndDemo.insertOne({"StudentName":"David", "StudentCountryName":"AUS"}); {   ... Read More

How to remove document on the basis of _id in MongoDB?

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

150 Views

To remove document on the basis of _id in MongoDB, implement the following syntaxdb.yourCollectionName.remove({“_id”:ObjectId(“yourId”});Let us first implement the following query to create a collection with documents>db.removeDocumentOnBasisOfId.insertOne({"UserName":"Larry", "UserAge":23, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986f9f330fd0aa0d2fe4a3") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Sam", "UserAge":21, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fb4330fd0aa0d2fe4a4") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Chris", "UserAge":24, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fc0330fd0aa0d2fe4a5") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fcf330fd0aa0d2fe4a6") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"David", "UserAge":28, "UserCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fed330fd0aa0d2fe4a7") }Following is the query to display all documents from a collection ... Read More

Increment a value in a MongoDB nested object?

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

368 Views

To increment a value in nested object, you can use $inc operator. Let us first implement the following query to create a collection with documents>db.incrementValueDemo.insertOne({"StudentName":"Larry", "StudentCountryName":"US", "StudentDetails":[{"StudentSubjectName":"Math", "StudentMathMarks":79}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986ca0330fd0aa0d2fe4a2") }Following is the query to display all documents from a collection with the help of find() method> db.incrementValueDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"),    "StudentName" : "Larry",    "StudentCountryName" : "US",    "StudentDetails" : [       {          "StudentSubjectName" : "Math",          "StudentMathMarks" : 79       }    ] ... Read More

Get the duplicate values of a field in MongoDB?

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

1K+ Views

Use aggregate() method to get the duplicate value of a field. Let us first create a collection with documents using the following query> db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995078863d6ffd454bb647") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995081863d6ffd454bb648") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995089863d6ffd454bb649") } > db.findAllNonDistinctDemo.insertOne({"UserName":"David", "UserAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995093863d6ffd454bb64a") } > db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c99509d863d6ffd454bb64b") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9950a7863d6ffd454bb64c") } ... Read More

Inserting the current datetime in MongoDB?

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

3K+ Views

To insert current datetime in MongoDB, use the $setOnInsert operator. Let us first implement the following query to create a collection with documents>db.addCurrentDateTimeDemo.insertOne({"StudentName":"John", "StudentAdmissionDate":new Date("2012-01-21") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae45330fd0aa0d2fe49f") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2013-05-24") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae54330fd0aa0d2fe4a0") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2019-07-26") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae5f330fd0aa0d2fe4a1") }Following is the query to display all documents from a collection with the help of find() method> db.addCurrentDateTimeDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c97ae45330fd0aa0d2fe49f"),    "StudentName" : "John",    "StudentAdmissionDate" : ISODate("2012-01-21T00:00:00Z") } { ... Read More

How to search document in MongoDB by _id

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

351 Views

To search a document in MongoDB by _id, you need to call ObjectId(). Let us first see the syntaxdb.yourCollectionName.find({"_id":ObjectId("yourId")}).pretty();To understand the concept and search a document, let us implement the following query to create a collection with documents> db.searchDocumentDemo.insertOne({"UserId":1, "UserName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8e4330fd0aa0d2fe487") } > db.searchDocumentDemo.insertOne({"UserId":2, "UserName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8ea330fd0aa0d2fe488") } > db.searchDocumentDemo.insertOne({"UserId":3, "UserName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8f1330fd0aa0d2fe489") } > db.searchDocumentDemo.insertOne({"UserId":4, "UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8fa330fd0aa0d2fe48a") } > db.searchDocumentDemo.insertOne({"UserId":5, "UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ... Read More

How to create double nested array in MongoDB?

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

274 Views

To create a double nested array in MongoDB, let us implement the query to create a collection with documents. Within that, we have created a double nested array that displays Student Details, with the project name and technologies used to develop the same project:> db.doubleNestedArrayDemo.insertOne( ... { ...    "StudentId" : "1000", ...    "StudentName" : "Larry", ...    "StudentDetails" : [ ...    { ...       "ProjectName" : "Online Banking", ...       "ProjectDetails" : [ ...       { ...          "TechnologyUsed" : "Java" ...       }, ...   ... Read More

Get the array of _id in MongoDB?

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

638 Views

The _id in MongoDB is a field, which is mandatory. In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. Following is the syntax to get the array of all the ids i.e. _id in MongoDBdb.yourCollectionName.find({ _id : { $in : [yourValue1, yourValue2, yourValue3, .......N] } } );Let us first implement the following query to create a collection with documents> db.selectInWhereIdDemo.insertOne({"_id":23}); { "acknowledged" : true, "insertedId" : 23 } > db.selectInWhereIdDemo.insertOne({"_id":28}); { "acknowledged" : true, "insertedId" : 28 } > db.selectInWhereIdDemo.insertOne({"_id":45}); { "acknowledged" : true, "insertedId" : 45 } > db.selectInWhereIdDemo.insertOne({"_id":75}); ... Read More

Get all the column names in a table in MongoDB

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

6K+ Views

In MongoDB, there is no concept of columns since MongoDB is schema-less and does not contain tables. It contains the concept of collections and a collection has different types of documents to store items.Let us see the syntax −db.yourCollectionName.insertOne({“YourFieldName”:yourValue, “yourFieldName”:”yourValue”, .......N});If you want a single record from a collection, you can use findOne() and in order to get all records from the collection, you can use find().The syntax is as follows −db.yourCollectionName.findOne(); //Get Single Record db.yourCollectionName.find(); // Get All RecordTo understand the above syntax, let us create a collection with the document. The query to create a collection with a ... Read More

Advertisements