Found 1659 Articles for Big Data Analytics

MongoDB query to fetch a document that does not have a particular field?

AmitDiwan
Updated on 30-Mar-2020 13:14:08

466 Views

To check for existence, use $exists. Let us create a collection with documents − > db.demo234.insertOne({"FirstName":"Chris", "LastName":"Brown", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e418a50f4cebbeaebec5148") } > db.demo234.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e418a5ff4cebbeaebec5149") } > db.demo234.insertOne({"FirstName":"John", "LastName":"Smith", Age:34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e418a70f4cebbeaebec514a") }Display all documents from a collection with the help of find() method −> db.demo234.find();This will produce the following output −{ "_id" : ObjectId("5e418a50f4cebbeaebec5148"), "FirstName" : "Chris", "LastName" : "Brown", "Age" : 24 } { "_id" : ObjectId("5e418a5ff4cebbeaebec5149"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ... Read More

How to limit the amount of characters returned from a field in a MongoDB?

AmitDiwan
Updated on 30-Mar-2020 13:12:40

359 Views

To limit the amount of characters returned from a field, use $substr in MongoDB. Let us create a collection with documents −> db.demo233.insertOne({"Paragraph":"My Name is John Smith.I am learning MongoDB database"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e41877df4cebbeaebec5146") } > db.demo233.insertOne({"Paragraph":"David Miller is a good student and learning Spring and Hibernate Framework."}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4187d7f4cebbeaebec5147") }Display all documents from a collection with the help of find() method −> db.demo233.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e41877df4cebbeaebec5146"),    "Paragraph" : "My Name is John Smith.I am learning MongoDB database" } ... Read More

Using MongoDB Aggregate and GroupBy to get the frequency of name record

AmitDiwan
Updated on 30-Mar-2020 13:09:36

110 Views

Let us first create a collection with documents −> db.demo232.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo232.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo232.insertOne({_id:103, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo232.insertOne({_id:104, Name:"David"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo232.insertOne({_id:105, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 105 }Display all documents from a collection with the help of find() method −> db.demo232.find();This will produce the following output −{ "_id" : 101, "Name" : "Chris" } { "_id" : 102, "Name" : "Bob" } { "_id" : ... Read More

Best way to sum array size fields in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 13:08:38

748 Views

To sum the array size fields, use $sum along with $size. Let us create a collection with documents −> db.demo231.insertOne({"Subjects":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc73ff4cebbeaebec5143") } > db.demo231.insertOne({"Subjects":["Java", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc757f4cebbeaebec5144") } > db.demo231.insertOne({"Subjects":["Python", "Spring"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc762f4cebbeaebec5145") }Display all documents from a collection with the help of find() method −> db.demo231.find();This will produce the following output −{ "_id" : ObjectId("5e3fc73ff4cebbeaebec5143"), "Subjects" : [ "MongoDB", "MySQL", "SQL Server" ] } { "_id" : ObjectId("5e3fc757f4cebbeaebec5144"), "Subjects" : [ "Java", "C", ... Read More

Usage of findOne() with MongoDB?

AmitDiwan
Updated on 30-Mar-2020 13:06:17

283 Views

The findOne() in MongoDB returns only a single document. Let us create a collection with documents −> db.demo230.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc4d2f4cebbeaebec513e") } > db.demo230.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc4d5f4cebbeaebec513f") } > db.demo230.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc4dbf4cebbeaebec5140") } > db.demo230.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc4dff4cebbeaebec5141") } > db.demo230.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc4e1f4cebbeaebec5142") }Display all documents from a collection with the help of find() method −> db.demo230.find();This will produce the following output −{ "_id" : ObjectId("5e3fc4d2f4cebbeaebec513e"), "FirstName" : ... Read More

Change a unique index to a sparse unique index in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 13:04:51

132 Views

For sparse index, use sparse:true. Following is the query to create an index −> db.demo229.ensureIndex({"ClientName":1}, {unique: true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Following is the query to display indexes −> db.demo229.getIndexes();This will produce the following output −[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_",       "ns" : "test.demo229"    },    {       "v" : 2,       "unique" ... Read More

Find document in MongoDB where at least one item from an array is not in the other?

AmitDiwan
Updated on 30-Mar-2020 13:02:20

114 Views

For this, set regex in MongoDB find(). Let us create a collection with documents −> db.demo228.insertOne({"Subjects":["MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fa51f03d395bdc213473b") } > db.demo228.insertOne({"Subjects":["MongoDB", "Java", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fa52c03d395bdc213473c") }Display all documents from a collection with the help of find() method −> db.demo228.find();This will produce the following output −{ "_id" : ObjectId("5e3fa51f03d395bdc213473b"), "Subjects" : [ "MongoDB", "Java" ] } { "_id" : ObjectId("5e3fa52c03d395bdc213473c"), "Subjects" : [ "MongoDB", "Java", "MySQL" ] }Following is the query to find documents where at least one item from an array is not in the ... Read More

Updating a set of documents from a list of key value pairs in MongoDB

AmitDiwan
Updated on 30-Mar-2020 13:00:16

400 Views

Let us create a collection with documents −> db.demo227.insertOne({"_id":"101", "Name":"Chris"}); { "acknowledged" : true, "insertedId" : "101" } > db.demo227.insertOne({"_id":"102", "Name":"Bob"}); { "acknowledged" : true, "insertedId" : "102" }Display all documents from a collection with the help of find() method −> db.demo227.find();This will produce the following output −{ "_id" : "101", "Name" : "Chris" } { "_id" : "102", "Name" : "Bob" }Following is the query to update a set of documents from a list of key value pairs −> var bulkUpdateValue = [{"_id": "101", "Name": "Robert"}, ...   {"_id": "102", "Name": "Sam"} ...]; > var bulkUpdate = db.demo227.initializeUnorderedBulkOp(); > ... Read More

MongoDB query to display all the values excluding the id?

AmitDiwan
Updated on 30-Mar-2020 12:58:57

445 Views

For this, use the $project. The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fieldsLet us first create a collection with documents −> db.demo226.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3f9be803d395bdc2134738") } > db.demo226.insertOne({"Name":"Bob", "Age":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3f9bf003d395bdc2134739") } > db.demo226.insertOne({"Name":"David", "Age":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3f9bf803d395bdc213473a") }Display all documents from a collection with the help of find() method −> db.demo226.find();This will produce the following ... Read More

Get the last two values with MongoDB

AmitDiwan
Updated on 30-Mar-2020 12:56:58

358 Views

To get the last two values, use MongoDB $slice. Let us create a collection with documents −> db.demo173.insertOne({"ListOfValues":[10,40,100,560,700,900]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383a4f9e4f06af551997e4") }Display all documents from a collection with the help of find() method −> db.demo173.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e383a4f9e4f06af551997e4"),    "ListOfValues" : [ 10, 40, 100, 560, 700, 900 ] }Following is the query to get the last two values −> db.demo173.find({}, { "ListOfValues": { "$slice": -2 } } );This will produce the following output −{ "_id" : ObjectId("5e383a4f9e4f06af551997e4"), "ListOfValues" : [ 700, 900 ] }

Advertisements