I'm trying to search my collection for all occurrences where the body property contains all of the search keywords.
Example string - "The black cat is definitely purple."
Keywords "black", "purple" would return the string.
Keywords "black", "dog" would not return that string.
I've been cruising some topics and Googling, but cannot seem to find the proper syntax to do this.
Currently, I am taking an string of keywords separated by commas, exploding it into an array, and then putting that into a MongoRegex Object. I know my syntax is off, because when I send just one keyword it works, but when there is more than one, I do not get any results that I would expect to get.
Current Approach:
<?php
function search_topics($array)
{
include_once('config.php');
$collection = get_connection($array['flag']);
$x = 0;
$string = null;
$search_results = null;
$keywords = explode(',', $array['search']);
$end_of_list = count($keywords);
while ($x < $end_of_list)
{
$string = $string."/".$keywords[$x];
$x++;
if($x >= $end_of_list)
{
$string = $string."/i";
}
}
if ($string != null)
{
try
{
$regex_obj = new MongoRegex($string);
$cursor = $collection->find(array('body' => $regex_obj));
}
catch (MongoCursorException $e)
{
return array('error' => true, 'msg' => $e->getCode());
}
foreach($cursor as $post)
{
$search_results[] = $post;
}
if ($search_results != null && count($search_results) > 1)
{
usort($search_results, 'sort_trending');
}
return array('error' => false, 'results' => $search_results);
}
else
{
return array('error' => false, 'results' => null);
}
}
?>
So, if I send the string black in $array['search'], my object is formed with /black/i and would return that string.
If I send the string black,cat in $array['search'], my object is formed with /black/cat/i and returns null.
Can anyone point me in the right direction with this regex syntax stuff?
Thanks in advance for any help!
Nathan
Instead of regular expressions, I would suggest you look at MongoDB's text search functionality instead, which is specifically made for situations like this: http://docs.mongodb.org/manual/core/text-search/
You would use that like this (on the MongoDB shell):
use admin
db.runCommand( { setParameter: 1, 'textSearchEnabled' : 1 } );
use test
db.so.ensureIndex( { string: 'text' } );
db.so.insert( { string: "The black cat is definitely purple." } );
db.so.runCommand( 'text', { search: '"cat" AND "dog"' } )
db.so.runCommand( 'text', { search: '"cat" AND "purple"' } )
A command doesn't return a cursor, but instead it will return one document containing all the query results in the results field. For the last search command, the result is:
{
"queryDebugString" : "cat|purpl||||cat|purple||",
"language" : "english",
"results" : [
{
"score" : 2.25,
"obj" : {
"_id" : ObjectId("51f8db63c0913ecf728ff4d2"),
"string" : "The black cat is definitely purple."
}
}
],
"stats" : {
"nscanned" : 2,
"nscannedObjects" : 0,
"n" : 1,
"nfound" : 1,
"timeMicros" : 135
},
"ok" : 1
}
In PHP, for the runCommand to turn on text search, you'd use:
$client->database->command( array(
'setParameter' => 1,
'textSearchEnabled' => 1
) );
And the text search itself as:
$client->database->command( array(
'text' => 'collectionName',
'search' => '"cat" AND "purple"'
) );
Related
i am trying to make a youtube video downloader with php it works fine, but if the youtube video id has a character like - or _ it shows an error undefined index url,it seems like php isn't seeing this characters e,g if the youtube video id is N65RvNkZFGE it will work but if its something like wXhTHyIgQ_U it wont work cause of the underscore here's the code:
// Check whether the url is valid
if(!empty($youtubeURL) && !filter_var($youtubeURL, FILTER_VALIDATE_URL) === false){
// Get the downloader object
$downloader = $handler->getDownloader($youtubeURL);
// Set the url
$downloader->setUrl($youtubeURL);
// Validate the youtube video url
if($downloader->hasVideo()){
// Get the video download link info
$videoDownloadLink = $downloader->getVideoDownloadLink();
$videoTitle = $videoDownloadLink[0]['title'];
$videoQuality = $videoDownloadLink[0]['qualityLabel'];
$videoFormat = $videoDownloadLink[0]['format'];
$videoFileName = strtolower(str_replace(' ', '_', $videoTitle)).'.'.$videoFormat;
$downloadURL = $videoDownloadLink[0]['url'];
$fileName = preg_replace('/[^A-Za-z0-9.\_\-]/', '', basename($videoFileName));
the YouTubeDownloader.class.php file which holds the setUrl() function, the get extractVideoId() here's the code:
public function setUrl($url){
$this->video_url = $url;
}
private function extractVideoId($video_url){
//parse the url
$parsed_url = parse_url($video_url);
if($parsed_url["path"] == "youtube.com/watch"){
$this->video_url = "https://www.".$video_url;
}elseif($parsed_url["path"] == "www.youtube.com/watch"){
$this->video_url = "https://".$video_url;
}
if(isset($parsed_url["query"])){
$query_string = $parsed_url["query"];
//parse the string separated by '&' to array
parse_str($query_string, $query_arr);
if(isset($query_arr["v"])){
return $query_arr["v"];
}
}
}
It has nothing to do with "_" or "-". "n85KukOXc0A" doesn't work either. Some videos don't return the "url" field, but "cipher". It's an attempt from YouTube to obfuscate the URL.
Your $videoDownloadLink, for the video ID "wXhTHyIgQ_U", looks like:
array (
0 =>
array (
'itag' => 18,
'bitrate' => 568627,
'width' => 640,
'height' => 360,
'lastModified' => '1575010363774854',
'contentLength' => '16085704',
'quality' => 'medium',
'qualityLabel' => '360p',
'projectionType' => 'RECTANGULAR',
'averageBitrate' => 568472,
'audioQuality' => 'AUDIO_QUALITY_LOW',
'approxDurationMs' => '226371',
'audioSampleRate' => '44100',
'audioChannels' => 2,
'cipher' => 's=__L8kZ2zTIc_OfmovvG91jyFU3WN4QTERuPCxA7rHfbHICEhCrCQkmqPth6pmfw5wmrIPOT_ijWceGCWdCeK-lVYXgIARwMGkhKDv&url=https%3A%2F%2Fr4---sn-hpa7kn7s.googlevideo.com%2Fvideoplayback%3Fexpire%3D1583898090%26ei%3DigloXtGYD4bngAeu8YXQCg%26ip%3D2a00%253Aee2%253A1200%253Ae400%253A8c11%253A6897%253A2e00%253Abef0%26id%3Do-AAcaOp-0syooPWmAUuzOfm6gHGPWYCiDlfa-RNdIP34W%26itag%3D18%26source%3Dyoutube%26requiressl%3Dyes%26mm%3D31%252C26%26mn%3Dsn-hpa7kn7s%252Csn-nv47lnly%26ms%3Dau%252Conr%26mv%3Dm%26mvi%3D3%26pl%3D32%26gcr%3Dsi%26initcwndbps%3D1023750%26vprv%3D1%26mime%3Dvideo%252Fmp4%26gir%3Dyes%26clen%3D16085704%26ratebypass%3Dyes%26dur%3D226.371%26lmt%3D1575010363774854%26mt%3D1583876448%26fvip%3D4%26fexp%3D23842630%26c%3DWEB%26txp%3D5531432%26sparams%3Dexpire%252Cei%252Cip%252Cid%252Citag%252Csource%252Crequiressl%252Cgcr%252Cvprv%252Cmime%252Cgir%252Cclen%252Cratebypass%252Cdur%252Clmt%26lsparams%3Dmm%252Cmn%252Cms%252Cmv%252Cmvi%252Cpl%252Cinitcwndbps%26lsig%3DABSNjpQwRQIgBvV2KI0zNTv-7PsmdoRnpyNBvxeMRJIHSlKjfScxihcCIQDlHa5A-1cGAVReyssZ4YkH2nV2rdN1fel6_-Bkv7CAjA%253D%253D&sp=sig',
'title' => 'Post Malone - Circles',
'mime' => 'video/mp4',
'format' => 'mp4',
),
)
As you see, there is no "url" field, but there is a "cipher" field.
If we decode it with parse_str($videoDownloadLink[0]['cipher'], $cipher) we get:
array (
's' => '__L8kZ2zTIc_OfmovvG91jyFU3WN4QTERuPCxA7rHfbHICEhCrCQkmqPth6pmfw5wmrIPOT_ijWceGCWdCeK-lVYXgIARwMGkhKDv',
'url' => 'https://r4---sn-hpa7kn7s.googlevideo.com/videoplayback?expire=1583898090&ei=igloXtGYD4bngAeu8YXQCg&ip=2a00%3Aee2%3A1200%3Ae400%3A8c11%3A6897%3A2e00%3Abef0&id=o-AAcaOp-0syooPWmAUuzOfm6gHGPWYCiDlfa-RNdIP34W&itag=18&source=youtube&requiressl=yes&mm=31%2C26&mn=sn-hpa7kn7s%2Csn-nv47lnly&ms=au%2Conr&mv=m&mvi=3&pl=32&gcr=si&initcwndbps=1023750&vprv=1&mime=video%2Fmp4&gir=yes&clen=16085704&ratebypass=yes&dur=226.371&lmt=1575010363774854&mt=1583876448&fvip=4&fexp=23842630&c=WEB&txp=5531432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cvprv%2Cmime%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=ABSNjpQwRQIgBvV2KI0zNTv-7PsmdoRnpyNBvxeMRJIHSlKjfScxihcCIQDlHa5A-1cGAVReyssZ4YkH2nV2rdN1fel6_-Bkv7CAjA%3D%3D',
'sp' => 'sig',
)
You need to properly scramble the "s" field value and add it to the URL as the field named with the "sp" field value.
The way it needs to be scrambled changes regularly. The current way from https://www.youtube.com/yts/jsbin/player_ias-vfle4a9aa/en_US/base.js is:
var Ps = function(a) {
a = a.split("");
Os.Dw(a, 1);
Os.hZ(a, 21);
Os.An(a, 24);
Os.hZ(a, 34);
Os.hZ(a, 18);
Os.hZ(a, 63);
return a.join("")
};
var Os = {
Dw: function(a, b) {
a.splice(0, b)
},
An: function(a) {
a.reverse()
},
hZ: function(a, b) {
var c = a[0];
a[0] = a[b % a.length];
a[b % a.length] = c
}
};
Which translates into PHP as:
function scramble($a) {
$a = str_split($a);
scr_splice($a, 1);
scr_swap($a, 21);
scr_reverse($a, 24);
scr_swap($a, 34);
scr_swap($a, 18);
scr_swap($a, 63);
return implode('', $a);
}
function scr_reverse(&$a) {
$a = array_reverse($a);
}
function scr_splice(&$a, $b) {
array_splice($a, 0, $b);
}
function scr_swap(&$a, $b) {
$c = $a[0];
$a[0] = $a[$b % count($a)];
$a[$b % count($a)] = $c;
}
In your code, you need to check which type of URL you got and get the proper URL.
if (isset($videoDownloadLink[0]['url'])) {
$downloadURL = $videoDownloadLink[0]['url'];
}
else if (isset($videoDownloadLink[0]['cipher'])) {
parse_str($videoDownloadLink[0]['cipher'], $cipher);
$downloadURL = $cipher['url']."&".$cipher["sp"]."=".scramble($cipher["s"]);
}
else {
die('Error getting YouTube URL!');
}
Note:
This will only work until YouTube changes the way it's scrambled again.
There is a function that displays categories ranging from the very top:
function getFullCategoryName($strCategoryId, $arrCategories)
{
$strCategoryIdPaent = NULL;
$arrCategoryCurr = isset($arrCategories[$strCategoryId]) ? $arrCategories[$strCategoryId] : NULL;
$arrCategoriesNames = [];
while (is_array($arrCategoryCurr)) {
$arrCategoriesNames[] = $arrCategoryCurr['title'];
if ($arrCategoryCurr['parentId'] && isset($arrCategories[$arrCategoryCurr['parentId']])) {
$arrCategoryCurr = $arrCategories[$arrCategoryCurr['parentId']];
} else {
$arrCategoryCurr = NULL;
}
}
krsort($arrCategoriesNames);
return implode(' > ', $arrCategoriesNames);
}
With just 3 array elements, I get an error:
"Allowed memory size of 134217728 bytes exhausted"
I understand that I am using something wrong. Please, help me understand what exactly.
This is my input array:
$arrCategories = array (
193450 =>
array (
'id' => '193450',
'parentId' => '193450',
'title' => 'Blood glucose meter',
),
193451 =>
array (
'id' => '193451',
'parentId' => '193450',
'title' => 'Sugar test strips',
),
193452 =>
array (
'id' => '193452',
'parentId' => '193452',
'title' => 'Blood glucose meter',
),
);
This is the call to the function:
$strCategoryId = 193450;
getFullCategoryName($strCategoryId, $arrCategories);
The while (is_array($arrCategoryCurr)) loop never ends as the else block of $arrCategoryCurr = NULL; is never called.
This happens because you have a loop where a node id is the same as his parent id. Look at your array:
....
'id' => '193450',
'parentId' => '193450',
...
To fix it modify the if statement to:
if ($arrCategoryCurr['parentId'] && $arrCategoryCurr['parentId'] != $arrCategoryCurr['id'] && isset($arrCategories[$arrCategoryCurr['parentId']])) {
Your (sample) data has an issue based on my reading of your function.
The parentId and index are the same in some items. This would create an infinite loop based on what I can work out from the question.
A better structure would be something like the following, with some error checking in the loop:
function getFullCategoryName($strCategoryId, $arrCategories) {
// set a base / default value
$arrCategoriesNames = [];
// do we even have anything to work with?
if (isset($arrCategories[$strCategoryId])) {
// at least one entry
do {
// get the title
$arrCategoriesNames[] = $arrCategories[$strCategoryId]['title'];
// get the next id and error check the data
if ((isset($arrCategories[$strCategoryId]['parentId'])) &&
($strCategoryId != $arrCategories[$strCategoryId]['parentId'])) {
// next index found and not the same
$strCategoryId = $arrCategories[$strCategoryId]['parentId'];
} else {
// either no parentId or a parentId that matches the current
// index. If that is the case, go no further.
$strCategoryId = false;
}
// you could add another error check if you like.
// if (count($arrCategoriesNames) == count($arrCategories)) {
// // go no further as data has a loop
// $strCategoryId = false;
// }
} while($strCategoryId);
// sort the data? why?
krsort($arrCategoriesNames);
}
// return a string
return implode(' > ', $arrCategoriesNames);
}
And testing you sample array;
$result = getFullCategoryName(193450,$arrCategories);
var_dump($result);
Returns the following:
string(19) "Blood glucose meter"
I'm new with MapReduce, but I have a collection that I'd like to apply myself to as a chance to learn how mapreduce works.
Example Documents:
{ "filename" : "resume.doc",
"folder" : "work",
"completed": "0.5" },
{ "filename" : "spreadsheet.xls",
"folder" : "work",
"completed": "0.6" },
{ "filename" : "thesis.doc",
"folder" : "school",
"completed": "0.75" },
{ "filename" : "coverletter.doc",
"folder" : "work",
"completed": "0.6"}
So the whole idea is: I'd like to query:
{ "folder" : "work",
"completed": { $gt: 0.5 },
"filename" : new MongoRegex( "/\.[a-zA-Z]{2,}$/" ) }
And ultimately get the count of all documents by extension (.doc, .xls, etc.), as so:
{ ".doc" : 1,
".xls" : 1 }
I also realize i've got an issue because my %-completed are strings, not floats, so i think mongodb is going to need more instruction for comparing the strings.
I'm using (if it matters):
PHP extension: mongo/1.5.7
MongoDB: version 3.2.11
Seems I've stumbled upon my own answer.
Let me know if anybody comes up with a more concise/expert solution.
But this seems to work.
try {
$map = new MongoCode(
'function(){
var re = new RegExp(/(.+)\.([a-zA-Z]{2,})$/);
doctype = this.value.match(re);
if(parseFloat(this.completed)>0.5){
emit(doctype[2], parseFloat(this.completed));
}
}'
);
$reduce = new MongoCode(
'function(key, values){
var sum = 0, num = 0;
for(var i in values){
if(parseFloat(values[i])>0.5){
sum += values[i];
num += 1;
}
}
return { number_of_documents : num,
sum_of_document_completions : sum,
average_completion : (sum/num) };
}'
);
$query = array (
"folder" => "work"
);
$doctypes = $db->command(
array(
'mapReduce' => 'mydocuments',
"map" => $map,
"reduce" => $reduce,
"query" => $query,
"out" => array("inline"=>1)
)
);
print_r ( $doctypes);
}
catch(MongoCursorException $e) {
echo "error message: ".$e->getMessage()."\n";
echo "error code: ".$e->getCode()."\n";
}
<?php
$json= '{
"fields" :
[
{
"name" : "news_title",
"type" : "text",
"value" : "old title"
},
{
"name" : "news_content",
"type" : "textarea",
"value" : "old content"
}
]
}';
echo $json;
$jsonInPHP = json_decode($json,true);
$results = count($jsonInPHP['fields']);
for ($r = 0; $r < $results; $r++){
// look for the entry we are trying to find
if ($jsonInPHP->fields[$r]['name'] == 'news_title'
&& $jsonInPHP->fields[$r]->value == 'old title'){
// remove the match
unset($jsonInPHP->fields[$r]);
if(empty($jsonInPHP->fields[$r]->value))
{
$jsonInPHP['fields'][$r]['name'] == 'news_title';
$jsonInPHP->fields[$r]->value=='no';
}
break;
}
}
function gog($status)
{
$results = count($status->fields);
for ($r = 0; $r < $results; $r++){
$status->fields[$r]->value == 'old rr';
}
}
$jsonInPHP->fields = array_values($jsonInPHP->fields);
echo json_encode($jsonInPHP);
?>
i want to change after searching from
'{"fields":[{"name":"news_title","type":"text","value":"old title"},{"name":"news_content","type":"textarea","value":"old content"}]}'
to
'{"fields":[{"name":"news_title","type":"text","value":"My new title"},{"name":"news_content","type":"textarea","value":"My new content"}]}'
Check official docs of json_decode Second parameter of function json_decode($json,true); (in your case true) determines whether result will be converted to associative array. If you want to use result as objects set that value to false or better omit that at all.
I want to insert data in Mongo database using PHP script, in year wise documents so that it may look like this (All years in one document);
cars{
2017{
car=Motorolla
color = blue
}
2016{
car=Toyota
color = green
}
2015{
car=Corolla
color = black
}
}
I wanted to add the document but it prompts
Document can't have $ prefixed field names: $years[0]
Is it possible to make such schema in Mongo using PHP?
Code
<?php
try {
$car = 'Motorolla';
$color = 'blue';
//$car = 'Toyota';
//$color = 'green';
//$car = 'Corolla';
//$color = 'black';
$years = array(2017, 2016, 2015);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, '$years[0]' => $car, '$years[1]' => $color]; // Making a query type
try {
$bulkWriteManager->insert($document); // Inserting Document
echo 1;
} catch(MongoCursorException $e) {
/* handle the exception */
echo 0;
}
$manager->executeBulkWrite('dbName.carsCol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
I do not want to add whole car object at once. I want to add Year object every time. Any help will be appreciable.
OR
Any relative answer so that I may get the data from Mongo Database according to the year?
Edit1
For first time creation. - Credits goes to #Veeram
<?php
try {
$car = 'Malibu';
$color = 'red';
$years = array(2017);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
//{"car":"chevy", "color":"black", year: 2017}
$insert = ['car' => $car, 'color' => $color, 'year' => $years[0]];
try {
$bulkWriteManager -> insert($insert); // Inserting Document
echo 1;
} catch (MongoCursorException $e) {
echo 0;
}
$manager->executeBulkWrite('dbName.mycol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
echo "In file:", $e->getFile(), "\n";
echo "On line:", $e->getLine(), "\n";
}
?>
For the updation- Credits goes to #Veeram
<?php
try {
$car = 'ChangedCar';
$color = 'changedColor';
$years = array(2017);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
$query = ['cars.year' => $years[0]];
//{ $push: { "cars.$.data": { "car":"chevy", "color":"black"} }}
$update = ['$push'=> ['cars.$.data'=>['car' => $car, 'color' => $color]]];
try {
$bulkWriteManager->update($query, $update); // Inserting Document
} catch(MongoCursorException $e) {
}
$manager->executeBulkWrite('dbName.mycol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
The problem in this code is that it successfully insert the data for the first time but when i update the data it does not update it.
Example:
There is a document named as cars . Insert the data with object of year in one document. Let's say the Object is 2017, it contains color and car Model. As showing below; (Multiple objects with years. Year is unique in whole document.)
cars{
2017{
car=Motorolla
color = blue
}
2016{
car=Toyota
color = green
}
2015{
car=Corolla
color = black
}
}
If I want to update just make an object of 2017 like 2017{car=Updated-Motorolla color =Updated-blue} and insert in the document. It should update only the year 2017 object in side the document.
cars{
2017{
car=Updated-Motorolla
color =Updated-blue
}
2016{
car=Toyota
color = green
}
2015{
car=Corolla
color = black
}
}
You can try something like this. Its not possible to perform all the Mongo db operations just based off key as a value.
The first solution is written to stay close to OP's design.
Assuming you can add a key to the year.
{
"cars": [{
"year": "2017",
"data": [{
"car": "Motorolla",
"color": "blue"
}]
}, {
"year": "2016",
"data": [{
"car": "Toyota",
"color": "green"
}]
}]
}
Makes it easy to reference the year by its value.
For example to add a new value into the data array for year 2017. You can try the below code.
Uses update positional $ operator.
query part to reference the array where 2017 record is stored.
update part using push to add the new car record to the existing data array for 2017 row.
<?php
try {
$car = 'Malibu';
$color = 'blue';
$years = [2017];
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
//{"cars.year":2017}
$query = ['cars.year' => $years[0]];
//{ $push: { "cars.$.data": { "car":"chevy", "color":"black"} }}
$update = ['$push'=> ['cars.$.data'=>['car' => $car, 'color' => $color]]];
try {
$bulkWriteManager->update($query, $update); // Update Document
echo 1;
} catch(MongoCursorException $e) {
/* handle the exception */
echo 0;
}
$manager->executeBulkWrite('dbName.carsCol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
For accessing data by year you can run below query.
Use query positional $operator to find the array index using the query part and reference that value in projection part.
db.collection.find({"cars.year":2017}, {"cars.$.data":1});
Alternative Solution :
This will take care of everything as just inserts
You are better off saving each car entry in its own document.
{ "year" : 2017, "car" : "Motorolla", "color" : "blue" }
{ "year" : 2016, "car" : "Toyota", "color" : "green" }
{ "year" : 2015, "car" : "Corolla", "color" : "black" }
For each entry you can use:
db.collection.insert({"year":2017, "car":"Motorolla", "color":"blue"});
PHP Code:
//{"car":"chevy", "color":"black", year: 2017}
$insert = ['car' => $car, 'color' => $color, 'year' => $years[0]];
try {
$bulkWriteManager - > insert($insert); // Inserting Document
echo 1;
} catch (MongoCursorException $e) {
/* handle the exception */
echo 0;
}
For access data by year you can use
db.collection.find({"year":2017});
Updated PHP code:
<?php
try {
$cars = ['Motorolla','Toyota', 'Corolla'] ;
$colors = ['blue', 'green', 'black'];
$years = [2017, 2016, 2015];
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
$query1 =["year" => $years[0]];
$query2 =["year" => $years[1]];
$query3 =["year" => $years[2]];
$update1 = ['$set' => ['car' => $cars[0], 'color' => $colors[0]]];
$update2 = ['$set' => ['car' => $cars[1], 'color' => $colors[1]]];
$update3 = ['$set' => ['car' => $cars[2], 'color' => $colors[2]]];
try {
$bulkWriteManager->update($query1, $update1, ["upsert" => true]);
$bulkWriteManager->update($query2, $update2, ["upsert" => true]);
$bulkWriteManager->update($query3, $update3, ["upsert" => true]);
echo 1;
} catch(MongoCursorException $e) {
/* handle the exception */
echo 0;
}
$manager->executeBulkWrite('dbName.carsCol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
You can perform complex queries using aggregation pipeline and you can add index to make your response quicker.
Observations:
First Solution : Harder to update/insert data, but keeps everything together so easier to read data.
Second Solution :
Cleaner and simpler to do CRUD operations on documents and use aggregation pipeline to preform complex queries.
Try to change
$document = ['_id' => new MongoDB\BSON\ObjectID, '$years[0]' => $car, '$years[1]' => $color];
to something like:
$document = ['_id' => new \MongoDB\BSON\ObjectID, $years[0] => ['car' => $car, 'color' => $color]];
it gives such result in mongo:
{ "_id" : ObjectId("58a936ecfc11985f525a4582"), "2017" : { "car" : "Motorolla", "color" : "blue" }
If data about all cars must be in one document, you need to combine data fitst:
$cars = [
'2017' => [
'car' => 'Motorolla',
'color' => 'blue'
],
'2016' => [
'car' => 'Toyota',
'color' => 'green'
],
'2015' => [
'car' => 'Corolla',
'color' => 'black'
]
];
and than
$document = ['_id' => new \MongoDB\BSON\ObjectID, 'cars' => $cars];
It gives mongo document like:
{ "_id" : ObjectId("58aabc0cfc11980f57611832"), "cars" : { "2017" : { "car" : "Motorolla", "color" : "blue" }, "2016" : { "car" : "Toyota", "color" : "green" }, "2015" : { "car" : "Corolla", "color" : "black" } } }