I have a more complex document "schema" saved in Mongo but the part that I need to match looks like this
"tags" : [
{
"tag" : "accompong maroon festival",
"type" : "label"
},
{
"tag" : "jamaica",
"type" : "label"
},
{
"tag" : "maroon warrior",
"type" : "label"
},
{
"tag" : "maroons",
"type" : "label"
},
{
"tag" : "caribbean culture",
"type" : "label"
},
{
"tag" : "rum",
"type" : "label"
}
}
I am using PHP to query the Mongo database and I have to query each document against an array of possible words.
array(
'boxing',
'warrior'
)
I don't know how to write the code in order to try to match the array that I have with the dataset saved in Mongo.
For now I only try to see if the tag is within the array of words
$data = $this->event_model->find_by(
array
(
'tags.tag' => array
(
'$in' => $Words
),
'published' => 'y'
)
);
I've resolved this problem by first creating an array that uses MongoRegex to search through the tags and by adding this array to the $or procedure
$or_array = array();
foreach($Words as $w)
{
$or_array[] = array(
'tags.tag' => new MongoRegex('/.*'. $w .'.*/i')
);
}
$data = $this->event_model->find_by(
array
(
'$or' => $or_array,
'published' => 'y'
)
);
Related
I'm having issues with the new mongodb driver for php.
While iterator_to_array works ok for ->find, if I only return one document it has MongoDB\Model\BSONDocument format.
$result = $db->clients->findOne(array('_id' => $id));
$result = (array) $result;
In Mongo the document looks something like:
{
"_id" : ObjectId("3894713i4b13iu412"),
"active" : true,
"logo" : "this is logo",
"settings" : {
"email" : "days",
"testMode" : false
},
"stats" : {
"f" : {
"all" : [
{
"count" : NumberLong(15846),
"sum" : NumberLong(149479)
},
{
"count" : NumberLong(15846),
"sum" : NumberLong(148891)
},
{
"count" : NumberLong(15846),
"sum" : {
"1" : NumberLong(15522),
"2" : NumberLong(324)
}
}
]
},
"l" : {
"order" : ISODate("2016-12-05T09:10:53.855+0000"),
"feedback" : ISODate("2016-12-05T08:34:48.403+0000")
}
},
"title" : "hhh.ro",
"url" : "sdfasdf"
}
This will only turn first level elements into an array, but nested document elements still remain MongoDB\Model\BSONDocument;
iterator_to_array doesn't work because this is not something iterable.
How do I convert entire document to an associative array like the old mongo driver or a stdClass?
you can set how the data is returned globally in database->setCollection options, Set typeMap to array
'typeMap' =>[
'document' => 'array',
'root' => 'array'
]
Link to Doc
I couldn't find the setCollection suggested by #Delcon in PHP MongoDB library so I used the below method.
typeMap option can also be set in the options array inside findOne.
$someCollection->findOne(
[],
['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]
);
Link to Doc
edit:
You could also pass the options array to the connector to set it globally as described by this Stackoverflow post.
$options = ["typeMap" => ['root' => 'array', 'document' => 'array']];
$conn = new MongoDB\Client("mongodb://localhost:27017", [], $options);
I am trying to update() a specific single array in a collection, but while it works fine with $push parameter on a single, specific array, it does not work with a $set parameter.
I don't quite understand logic behind that, because when I use such an example of $pushing the element:
$post_comment = array('$push' =>
array("comments" => array(
"_id" => new MongoId(),
"comment" => htmlspecialchars($_POST['comment']),
"author" => $user->username,
"date" => new MongoDate()
)
)
);
$entries->update(array(
"_id" => $_GET["id"]), $post_comment);
It gives me an array in a MongoDB database which looks more or less like this (with four items pushed in, respectively) :
{
"_id" : "css-clearfix-explained",
"comments" : [
{
"_id" : ObjectId("540cc940af105b19133c9869"),
"comment" : "aaa",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:08:16.215Z")
},
{
"_id" : ObjectId("540cc943af105b19133c986a"),
"comment" : "bbb",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:08:19.542Z")
},
{
"_id" : ObjectId("540cc946af105b19133c986b"),
"comment" : "ccc",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:08:22.968Z")
}
]
}
Which is basically what I want to have, and logically, works fine according to the documentation. But when I try the same with $set as for to edit an individual comment, in the similar fashion as shown:
$edit_comment = array('$set' =>
array("comments" => array(
"_id" => new MongoId($_POST['cmt-id']),
"comment" => htmlspecialchars($_POST['edit-comment']),
"author" => $user->username,
"date" => new MongoDate()
)
)
);
$entries->update(array(
"_id" => $_GET["id"]), $edit_comment);
It outputs four different arrays in place of the previous arrays, to illustrate that, i'll show what happened when I updated first comment "aaa" to "ddd" :
{
"_id" : "css-clearfix-explained",
"comments" : {
"_id" : ObjectId("540cc940af105b19133c9869"),
"comment" : "ddd\r\n ",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:12:10.833Z")
}
}
All the four array elements were pretty much erased and in their place appeared four fields as four independent array elements.
How come? Shouldn't it just work just fine like the example with $push above?
You didn't specify an index within comments. Therefore, $set replaced the array comments with the associated array supplied.
If you want to update a comment, then change your query in the first argument to match a comment by a unique field. Ex, date. In the second argument use a positional $ operator.
Example:
$edit_comment = array('$set' =>
array("comments.$" => array(
"_id" => new MongoId($_POST['cmt-id']),
"comment" => htmlspecialchars($_POST['edit-comment']),
"author" => $user->username,
"date" => new MongoDate()
)
)
);
// this assumes the post date is unique. On second though use something else.
$query = array( "_id" => $_GET["id"], "comments.date" => $_POST['post-date'])
$entries->update( $query, $edit_comment);
Check this out for more info and better explanation:
MongoDB - $set to update or push Array element
I need to find in array saved in a document of Mongodb.
Here is the document :
{
"hashtag" : "World",
"topimages" : [
{
"cluster" : "france",
"id_tw" : "477170636327227393"
},
{
"cluster" : "france",
"id_tw" : "477170636327227396"
}
]
}
And I want to search if a document named "World" by key "hashtag", have already a tweet saved in the array "topimages" with an id "id_tw" of value : "477170636327227393"
What I'm trying to do is :
$query = array('hashtag' => "World", array('topsimages.$.id_tw' => "477170636327227393"));
$xpmm->find($query);
I need to search if a document called "World" has already a tweet saved in the array "topimages" by his key "id_tw".
Thanks a lot.
#
Solution :
$query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464");
That's ok using :
$query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464");
topimages.x would only work if it was a subdocument, not an array. You want $elemMatch (Mongo DB Documentation)
$query = array('hashtag' => "World", 'topsimages' => array("$elemMatch" => array("id_tw" => "477170636327227393")));
I am trying to use a jQuery gantt as a wordpress plugin. Currently I'm stuck on editing the data.json. I use a php form to populate a new item. When submitting the form, it will add data to the file, but behind the closing square brackets.
[{
...
},
{ "name" : "Vermessung"
, "desc" : ""
, "values": [
{ "id" : "5"
, "from" : "/Date(1363132800000)/"
, "to" : "/Date(1368655200000)/"
, "desc" : "Vom Beauftragen der Vermessung bis zur tatsächlichen Vermessung"
, "customClass": "ganttBlue"
, "label" : "Vermessung"
}
]
}
]
After submitting the form it looks like this:
[{
...
},
{ "name" : "Vermessung"
, "desc" : ""
, "values": [
{ "id" : "5"
, "from" : "/Date(1363132800000)/"
, "to" : "/Date(1368655200000)/"
, "desc" : "Vom Beauftragen der Vermessung bis zur tatsächlichen Vermessung"
, "customClass": "ganttBlue"
, "label" : "Vermessung"
}
]
}
]{"name":null,"desc":null,"values":{"id":null,"from":null,"to":null,"desc":null,"customClass":null,"label":null}}
This is the requested php Code which will adding stuff to the json:
$file = jQg_BASENAME_DIR.'/inc/data.json';
log_me('This is a message for debugging purposes');
if(isset($_POST['submit'])){
$json = file_get_contents( $file );
$data = json_decode($json);
// convert form data to json format
$postArray = array(
"name" => $_POST['name'],
"desc" => $_POST['desc'],
"values" => array(
"id" => $_POST["value_id"],
"from" => $_POST['value_from'],
"to" => $_POST['value_to'],
"desc" => $_POST['value_desc'],
"customClass" => $_POST['value_class'],
"label" => $_POST['value_label']
)
); //you might need to process any other post fields you have..
$json = json_encode( $postArray );
array_push($json, $postArray);
// write to file
file_put_contents( $file, $json, FILE_APPEND);
I also can't establish the square bracket after value. How can I fix this?
As I said in my comment
$json = file_get_contents( $file );
// $json is now a string
$data = json_decode($json);
// $data is a PHP object
// So lets call the second array $data->someArray
// since I do not know what it is called looking at your file
// convert form data to PHP array format
$postArray = array(
"name" => $_POST['name'],
"desc" => $_POST['desc'],
"values" => array(
"id" => $_POST["value_id"],
"from" => $_POST['value_from'],
"to" => $_POST['value_to'],
"desc" => $_POST['value_desc'],
"customClass" => $_POST['value_class'],
"label" => $_POST['value_label']
)
); //you might need to process any other post fields you have..
// $postArray is a PHP object
// $json = json_encode( $postArray ); // do NOT convert here
array_push($data->someArray, $postArray);
$json = json_encode($data);
// write to file
file_put_contents( $file, $json, FILE_APPEND);
Your values field is an array of objects instead of an object (the encoding of a php associative array is a json oject). So for values to have square brackets instead of "values" => array() you would need "values" => array( array("id" => ... etc. ) )
As for your first problem you've inverted the json_encoding. First push your postArray into data, then json_encode data.
Lets say we have a document like this
{
"_id" : "1234",
"Data" : {
"Name" : "Pythagoras",
"Like" : "Math"
}
And we changed over mind and want to push more things to Data->Like so it looks like;
{
"_id" : "1234",
"Data" : {
"Name" : "Pythagoras",
"Like" : ["Math", "Science"]
}
All the atomic operators like $push, $pushAll and $addToSet works just when Data->Like already is an array.
I´m using the php-driver. In this example there is no meaning to not set the Data->Like to an array at the beginning but it does not work like that in my code ;(
Hope you can help me and sorry for my bad English ;) Thanks!
You'll have to iterate over all your documents and change the value to an array. For example, you can do that with:
$m = new Mongo();
$c = $m->yourdbname->yourcollectionname;
foreach ( $c->find() as $r )
{
if ( !is_array( $r['Data']['Like'] ) )
{
$c->update( array( '_id' => $r['_id'] ), array( '$set' => array( 'data.like' => array( $r['data']['like'] ) ) ) );
}
}