Saturday, 31 August 2019

MongoDB operators with examples

MongoDB operators with examples

Comparison Query Selectors

$eq  Equal to a specified value.
$gt  Greater than a specified value.
$gte  Greater than or equal to a specified value.
$lt  Less than a specified value.
$lte  Less than or equal to a specified value.
$ne  Not equal to a specified value.
$in  Matches any of the values specified in an array.
$nin  Matches none of the values specified in an array.

Example:
db.mycollectiontest.find('{"likes": {$gt:10}"likes"}'); 


Logical Query Selectors
$or  Joins query clauses with a logical OR Operator.
$and  Joins query clauses with a logical AND Operator.
$not  Inverts the effect of a query expression.
$nor  Joins query clauses with a logical NOR returns.

Example
db.mycollectiontest.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"})


Element Query Selectors
$exists  Matches documents that have the specified field.
$type  Selects documents if a field is of the specified type.



Evalution Query Selector
$mod  Performs a modulo operation.
$regex  Specified regular expression.
$text  Performs text search.
$where  Matches documents that satisfy a JavaScript expression.



Array Query Selector
$all        Matches arrays that contain all elements specified in the query.
$elemMatch Selects documents if element in the array field matches all the specified $elemMatch conditions.
$size       Selects documents if the array field is a specified size.


Field Update Operators
$inc        Increments the value.
$mul        Multiplies the value.
$rename     Renames a field.
$setOnInsert Sets the value of a field if an update results in an insert of a document.
$set  Sets the value of a field in a document.
$unset  Removes the specified field from a document.
$min  Only updates the field if the specified value is less than the existing field value.
$max  Only updates the field if the specified value is greater than the existing field value.
$currentDate  Sets the value of a field to current date, either as a Date or a Timestamp.





Monday, 19 August 2019

MongoDb PHP functions Questions and Answers

MongoDb PHP functions Questions and Answers

Question: How to connect to MongoDb with PHP?
$mongoObj = new MongoClient();



Question: How to select database?
$mongoDbObj = $mongoObj->mydb;



Question: How to create collection?
$collection = $mongoDbObj->createCollection("mycollectiontest");



Question: How to select collection?
$collection= $mongoDbObj->mycollectiontest;



Question: How to insert document in collection?
/* 1 document */
$document = array( 
      "title" => "MongoDB", 
      "description" => "MongoDB database", 
      "likes" => 100,
      "unlikes", 50
   );
   $collection->insert($document);
/* 2 document */
$document = array( 
      "title" => "MongoDB2", 
      "description" => "MongoDB database2", 
      "likes" => 1002,
      "unlikes", 502
   );
   $collection->insert($document); 



Question: How to list all documents?
   $cursor = $collection->find();
   foreach ($cursor as $document) {
      print_r($document);
   }

Output
Array
(
    [_id] => MongoId Object
        (
            [$id] => 57c3e73d9ba8827012000029
        )

    [title] => MongoDB
    [description] => MongoDB database
    [likes] => 100
    [0] => unlikes
    [1] => 50
)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 57c3e7e79ba8821815000029
        )

    [title] => MongoDB2
    [description] => MongoDB database2
    [likes] => 1002
    [0] => unlikes
    [1] => 502
)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 57c3e8fb9ba882181500002a
        )

    [title] => MongoDB2
    [description] => MongoDB database2
    [likes] => 1002
    [0] => unlikes
    [1] => 502
)



Question: How to list all documents (2nd way)?
 $cursor = $mongoObj->mydb->{'mycollectiontest'}->find();
/*$mongoObj is clinet, mydb is database, mydb is collections*/
   foreach ($cursor as $document) {
      print_r($document);
   }



Question: How to list all documents (3rd way)?
$query = array('title' => 'MongoDB');
$cursor = $collection->find($query);
   foreach ($cursor as $document) {
      print_r($document);
   }



Question: How to delete a document?
 $collection->remove(array("title"=>"MongoDB2"));



Question: How to update a document?
  $collection->update(array("title"=>"MongoDB"), 
 array('$set'=>array("description"=>"This is updated description")));