Database Articles

Page 225 of 546

Restrict returned data in MongoDB to display only specific values from documents

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 425 Views

To restrict returned data, use find(). The values 0 and 1 for fields would decide what all field values would be visible or hidden.Let us create a collection with documents −> db.demo330.insertOne({"Id":101, "Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e52149ff8647eb59e562081") } > db.demo330.insertOne({"Id":102, "Name":"Sam", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5214aaf8647eb59e562082") } > db.demo330.insertOne({"Id":103, "Name":"David", "Age":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5214b3f8647eb59e562083") } > db.demo330.insertOne({"Id":104, "Name":"Bob", "Age":23}); . {    "acknowledged" : true,    "insertedId" : ObjectId("5e5214bdf8647eb59e562084") }Display all documents from a collection with the help of find() method −> db.demo330.find();This ...

Read More

Using findOneAndUpdate () to update in MongoDB?

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 365 Views

The findOneAndUpdate() is used to update a single document based on the filter and sort criteria i.e. −db.collection.findOneAndUpdate(filter, update, options)Let us create a collection with documents −> db.demo328.insertOne({Name:"Chris", Marks:67}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516b19f8647eb59e56207a") } > db.demo328.insertOne({Name:"David", Marks:78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516b24f8647eb59e56207b") } > db.demo328.insertOne({Name:"Bob", Marks:97}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516b2bf8647eb59e56207c") }Display all documents from a collection with the help of find() method −> db.demo328.find();This will produce the following output −{ "_id" : ObjectId("5e516b19f8647eb59e56207a"), "Name" : "Chris", "Marks" : 67 } { "_id" : ObjectId("5e516b24f8647eb59e56207b"), "Name" : ...

Read More

MongoDB query to set user defined variable into query?

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 504 Views

For user-defined variables, use var keyword in MongoDB. Let us create a collection with documents −> db.demo327.insertOne({"FirstName":"Chris", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516952f8647eb59e562076") } > db.demo327.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e51695af8647eb59e562077") } > db.demo327.insertOne({"FirstName":"John", "LastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516962f8647eb59e562078") } > db.demo327.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516968f8647eb59e562079") }Display all documents from a collection with the help of find() method −> db.demo327.find();This will produce the following output −{ "_id" : ObjectId("5e516952f8647eb59e562076"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5e51695af8647eb59e562077"), ...

Read More

Accessing array values in a MongoDB collection to fetch a specific document

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 275 Views

To access array values, use dot(.) notation. Let us create a collection with documents −> db.demo326.insertOne({id:101, "ProductDetails":[{"ProductId":"Prod-101", "ProductName":"Product-1"}, ... {"ProductId":"Prod-102", "ProductName":"Product-2"} ... ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516751f8647eb59e562074") } > db.demo326.insertOne({id:103, "ProductDetails":[{"ProductId":"Prod-104", "ProductName":"Product-5"}, ... {"ProductId":"Prod-105", "ProductName":"Product-6"} ... ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516771f8647eb59e562075") }Display all documents from a collection with the help of find() method −> db.demo326.find();This will produce the following output −{    "_id" : ObjectId("5e516751f8647eb59e562074"), "id" : 101, "ProductDetails" : [       { "ProductId" : "Prod-101", "ProductName" : "Product-1" }, ...

Read More

MongoDB many insertsupdates without affecting performance?

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 219 Views

Use insertMany() to insert multiple documents into a collection. With that, to update performance you can use ensureIndex().Let us create a collection with documents and insert multiple documents −> db.demo325.insertMany( [ ...    { _id: 101, Name: "Chris", Age: 23 }, ...    { _id: 102, Name: "David", Age: 24 }, ...    { _id: 103, Name: "Bob", Age: 22 } ... ] ); { "acknowledged" : true, "insertedIds" : [ 101, 102, 103 ] }Display all documents from a collection with the help of find() method −> db.demo325.find().pretty();This will produce the following output −{ "_id" : 101, "Name" : "Chris", "Age" : 23 } { "_id" : 102, "Name" : "David", "Age" : 24 } { "_id" : 103, "Name" : "Bob", "Age" : 22 }Using ensureIndex() −> db.demo325.ensureIndex({Name:1});This will produce the following output −{    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }

Read More

Using positional operator with hierarchies in MongoDB?

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 138 Views

Let us create a collection with documents −> db.demo324.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516349f8647eb59e562073") }Display all documents from a collection with the help of find() method −> db.demo324.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e516349f8647eb59e562073"),    "ListOfValues" : [       10,       20,       30    ] }Here is the query displaying how to use positional operator −> db.demo324.update({"ListOfValues":[10, 20, 30]}, { $set: { "ListOfValues.$[]": "MongoDB" } }, { upsert: true } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all ...

Read More

Array index or indexing inner items in MongoDB to fetch values

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 154 Views

At first, let us create a collection with documents and also use ensureIndex() to create an index −> db.demo323.insertOne({"details":{"Name":"Chris", "Age":34}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e51157af8647eb59e56206e") } > db.demo323.insertOne({"details":{"Name":"David", "Age":31}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e511581f8647eb59e56206f") } > db.demo323.insertOne({"details":{"Name":"Bob", "Age":28}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e511589f8647eb59e562070") } > db.demo323.ensureIndex({"details.Name":1}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 2,    "numIndexesAfter" : 3,    "ok" : 1 }Display all documents from a collection with the help of find() method −> db.demo323.find();This will produce the following output −{ "_id" : ObjectId("5e51157af8647eb59e56206e"), "details" : ...

Read More

Check for existing documents/embedded documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 287 Views

To check for existing documents/embedded documents, use $exists in MongoDB. Let us create a collection with documents −> db.demo322.insertOne( ...  {'id':1001, ...    'details':[{'Score':10000, Name:"Bob"}, ...       {'Score':98000, Name:"Sam"} ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5113e2f8647eb59e56206c") } > db.demo322.insertOne( ... {'id':10002, ...    'details':[{'Score':9000}, ...       {'Score':91000} ...       ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5113faf8647eb59e56206d") }Display all documents from a collection with the help of find() method −> db.demo322.find();This will produce the following output −{    "_id" ...

Read More

How to store array values in MongoDB?

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 2K+ Views

Let us first create a collection with documents wherein we are storing array values −>db.demo321.insertOne({"UserDetails":[{"UserId":101, "UserName":"Chris"}, {"UserId":102, "UserName":"Mike"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e511248f8647eb59e56206a") } >db.demo321.insertOne({"UserDetails":[{"UserId":103, "UserName":"Bob"}, {"UserId":104, "UserName":"Sam"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e511259f8647eb59e56206b") }Display all documents from a collection with the help of find() method −> db.demo321.find();This will produce the following output −{ "_id" : ObjectId("5e511248f8647eb59e56206a"), "UserDetails" : [ { "UserId" : 101, "UserName" : "Chris" }, { "UserId" : 102, "UserName" : "Mike" } ] } { "_id" : ObjectId("5e511259f8647eb59e56206b"), "UserDetails" : [ { "UserId" : 103, "UserName" : "Bob" }, ...

Read More

MongoDB query for capped sub-collection in an array

AmitDiwan
AmitDiwan
Updated on 02-Apr-2020 462 Views

In MongoDB, you cannot use capped for sub-collection. However, use capped on the overall document. To display a specific number of values from an array, prefer $slice.Let us create a collection with documents −> db.demo319.insertOne({"Scores":[100, 345, 980, 890]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ecf6f8647eb59e562064") } > db.demo319.insertOne({"Scores":[903, 10004, 84575, 844]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ed01f8647eb59e562065") }Display all documents from a collection with the help of find() method −> db.demo319.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e50ecf6f8647eb59e562064"),    "Scores" : [       100,       345,     ...

Read More
Showing 2241–2250 of 5,456 articles
« Prev 1 223 224 225 226 227 546 Next »
Advertisements