Meaning and Usage of & Assignment Operator in PHP

AmitDiwan
Updated on 01-Jul-2020 07:10:22

868 Views

Instead of copying the data from one variable to another one, the changes made to an array or object can be made to another using the ‘=&’ operator. This is known as the ‘assignment by reference’ method, which means both the values or objects will point to the same data, and no copies of the data are made. This way, data redundancy is avoided.Example Live DemoOutputThe value is : 89Inside the tags, two values are declared wherein the second value is the assignment by reference of the first value. Next, the value of the first variable is changed and the ... Read More

Iterate Over Characters of a String in Python

Pavitra
Updated on 01-Jul-2020 07:10:14

2K+ Views

In this article, we will learn about iterating/ traversing over characters of a string in Python 3.x. Or earlier.The string is a collection of characters which may contain spaces, alphabets or integers. They can be accessed using indexes or via references . Some commonly implemented methods are shown below.Method 1 − The direct itertor without indexingExamplestring_inp = "tutorialspoint" # Iterate over the string for value in string_inp:    print(value, end='')Method 2 − The most common way using index based accessExamplestring_inp = "tutorialspoint" # Iterate over the string for value in range(0, len(string_inp)):    print(string_inp[value], end='')Method 3 − The ... Read More

MongoDB Aggregate Group Multiple Result

AmitDiwan
Updated on 01-Jul-2020 07:03:26

752 Views

To aggregate multiple result, use $group in MongoDB. Let us create a collection with documents −> db.demo765.insertOne( ... ...    { ...       Name:"John", ...       "Category":"ComputerScience", ...       "SubjectName":"MongoDB", ...       "Marks":75 ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb054525637cd592b2a4b01") } > > db.demo765.insertOne( ...    { ...       Name:"John", ...       "Category":"ComputerScience", ...       "SubjectName":"MySQL", ...       "Marks":85 ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb054525637cd592b2a4b02") } > db.demo765.insertOne( ... ... Read More

Checking for Not Null in MongoDB

AmitDiwan
Updated on 01-Jul-2020 07:00:01

799 Views

Use $ne to check for not null. Let us create a collection with documents −> db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":"Chris_12"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb04ee55637cd592b2a4afc") } > db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb04eee5637cd592b2a4afd") } > db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb04ef35637cd592b2a4afe") }Display all documents from a collection with the help of find() method −> db.demo764.find();This will produce the following output −{ "_id" : ObjectId("5eb04ee55637cd592b2a4afc"), "LoginUserName" : "Chris", "LoginPassword" : "Chris_12" } { "_id" : ObjectId("5eb04eee5637cd592b2a4afd"), "LoginUserName" : "Chris", "LoginPassword" : null } { "_id" : ObjectId("5eb04ef35637cd592b2a4afe"), "LoginUserName" : "Chris", ... Read More

Querying Nested Documents in MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:58:37

1K+ Views

To query on an array of objects for nested documents, use find(). Let us create a collection with documents −> db.demo763.insertOne( ...    { ...       _id:1, ...       CountryName:"US", ...       "studentInformation": [ ...          { ...             StudentName:"Chris", ...          }, ...          { ...             StudentName:"David", ...             StudentAge:22 ...          } ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : 1 }Display all documents from a collection with the help of find() method −> db.demo763.find();This will produce the following output −{ "_id" : 1, "CountryName" : "US", "studentInformation" : [ { "StudentName" : "Chris" }, { "StudentName" : "David", "StudentAge" : 22 } ] }Following is how to query an array of objects to fetch specific nested documents −> db.demo763.find({}, ... { ...    studentInformation: { ...       $elemMatch: { ...          StudentAge: { ...             $exists: true ...          } ...       } ...    } ... })This will produce the following output −{ "_id" : 1, "studentInformation" : [ { "StudentName" : "David", "StudentAge" : 22 } ] }

MongoDB Aggregation and Projection

AmitDiwan
Updated on 01-Jul-2020 06:57:21

1K+ Views

For this, use $project along with aggregate(). The $project in aggregation passes along the documents with the requested fields to the next stage in the pipeline.Let us create a collection with documents −> db.demo762.insertOne({ ...    "_id" : { ...       "userId":101, ...       "userName":"Chris" ...    }, ..   . "countryName" : "US", ... ...    "details" : [ ...       { ...          "Name" : "Robert", ...          "DueDate" : "2020-04-10" ... ...       }, ... ...       { ...     ... Read More

Query an Array in MongoDB to Fetch a Specific Value

AmitDiwan
Updated on 01-Jul-2020 06:54:10

660 Views

To fetch a specific value from an array, use aggregate() along with $project. Let us create a collection with documents −> db.demo761.insertOne( ...    { ...       "details": [ ...          { ...             "student": { ...                "FullName": "Chris Brown" ...             } ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb034d15637cd592b2a4af1") } > db.demo761.insertOne( ...    { ...       ... Read More

Update Many Documents with One Query in MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:52:18

596 Views

To update many documents with a single query, use bulkWrite() in MongoDB. Let us create a collection with documents −> db.demo760.insertOne({id:1, details:{Value1:100, Value2:50}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb0309f5637cd592b2a4aee") } > db.demo760.insertOne({id:2, details:{Value1:60, Value2:70}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb030a15637cd592b2a4aef") } > db.demo760.insertOne({id:3, details:{Value1:80, Value2:90}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb030a15637cd592b2a4af0") }Display all documents from a collection with the help of find() method −> db.demo760.find();This will produce the following output −{ "_id" : ObjectId("5eb0309f5637cd592b2a4aee"), "id" : 1, "details" : { "Value1" : 100, "Value2" : 50 } } { "_id" : ... Read More

MongoDB Regex Operator $i for Case-Insensitive Search

AmitDiwan
Updated on 01-Jul-2020 06:49:04

574 Views

For this, you need to use case insensitive (i). Let us create a collection with documents −> db.demo759.insertOne({SubjectName:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb02ba95637cd592b2a4ae7") } > db.demo759.insertOne({SubjectName:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb02baa5637cd592b2a4ae8") } > db.demo759.insertOne({SubjectName:"mongodb"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb02baf5637cd592b2a4ae9") } > db.demo759.insertOne({SubjectName:"MONGODB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb02bb85637cd592b2a4aea") }Display all documents from a collection with the help of find() method −> db.demo759.find();This will produce the following output −{ "_id" : ObjectId("5eb02ba95637cd592b2a4ae7"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eb02baa5637cd592b2a4ae8"), "SubjectName" : "MongoDB" } { "_id" ... Read More

Convert One Record with an Array to Multiple Records in MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:47:20

515 Views

For this, you can use $out along with aggregate() and $unwind. Let us create a collection with documents −> db.demo757.insertOne( ...    { ...       "id": 101, ...       "Name": ["John", "Bob", "Chris"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb025745637cd592b2a4ae2") } > db.demo757.insertOne( ...    { ...       "id": 102, ...       "Name": ["David"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb025755637cd592b2a4ae3") }Display all documents from a collection with the help of find() method −> db.demo757.find();This will produce the ... Read More

Advertisements