This is the structure for my MongoDB documents
{
"_id" : ObjectId("5aa02da4f6c9bf20da58017b"),
"Name" : "HP Envy",
"Model" : "m6 convertible x360",
"Cost" : 1350,
"Description" : "Sleek and slim laptop designed perfectly for business p
urposes",
"Keywords" : [
"Touchscreen",
"Rotation",
"x360",
"Slim",
"HP"
],
"Reviews" : [
{
"User" : "User 1",
"Comment" : "Great Laptop",
"Rating" : 4.5
},
{
"User" : "User 2",
"Comment" : "Great Laptop",
"Rating" : 4.5
}
]
}
I want to access the data contained inside "Reviews" using php. I can access it normal using
$coll=$db->Product;
$cursor = $coll->find();
and then echo using a foreach to display in a tabular format. But i only know how to access the ID, name etc. How can i access the Reviews inside the object arrays? i tried something like this:
$test=$coll->find(array( 'Reviews.User' => 'User 1' ));
when i try to echo it it gives me an error: Catchable fatal error: Object of class MongoCursor could not be converted to string
I'm lost please help.. I apologize if i repeated the question but i couldn't find it anywhere else
Related
I am trying to create a json representation of mysql data in a restful webservice using php. my returned json should look something like this:
"worktypes" :
[
{
"id" : "1",
"name" : "نوع 1",
"isactive" : "true"
},
{
"id" : "2",
"name" : "نوع 2",
"isactive" : "false"
}
]
one of my options was to iterate through my result records and echo each line by hand. It was working until I reached utf8 characters shown above (like 'نوع 1') and instead of ending up with something like \u00d9\u0086\u00d9\u0088\u00d8\u00b9 I got unreadable symbols like ÃÂÃÂù çÃÂÃÂ.
If I could use json_encode somehow to create this layout, it would automatically solve this problem;But I don't know how to create such a structure in php that its json_encoded string represents my desired layout.
My other option is to somehow encode these characters myself. But I don't know how to do that either.
Could anyone help me with my problem?
UPDATE: I use a simple echo in my webservice when I get unusual characters like :
$result = array("1" => "نوع 1")
echo $result;
//results in 'ÙÙع 1' when called using webservice
The equivalent PHP array to what you have given is:
$x = array(
"worktypes" => array(
array(
"id" => "1",
"name" => "نوع 1",
"isactive" => "true"
),
array(
"id" => "2",
"name" => "نوع 2",
"isactive" => "false"
)
)
);
You can then use json_encode.
Note that you may still see escaped characters, but the browser should be able to handle unescaping them when it parses the JSON.
i actually have a issue with mongodb im trying to update/add a value in a nested array.
{
"_id" : ObjectId("56c37e98aff662100900002a"),
"name" : "michell",
"game" : [{
"name" : "GTA",
"badges" : [{
"name" : "pacifist"
}, {
"name" : "killemall"
}]
}]
}
you can find below the way i tried but actually it just rewrite the badges array of create new game array
$collection->update(array('_id' =>new MongoId($id),'jeux.name'=>$name), array('$set' => array('jeux'=>array('name' => $name,'badges'=>array('name'=>$badge)))));
i can't find the issue here if you could help me
Well i found a solution i don't know if its the best one but it seems like it work
$collection->update(array('_id' =>new MongoId($id),'jeux.name'=>$name), array('$push' => array('jeux.$.badges'=>array('name'=>$badge))));
im using the $ in jeux.$.badges
I am new to MongoDb and I've made a simple PHP filtering application (similar to TAGS)
I Have a collection which is called "Filters"
Inside of it, each record looks like this:
"_id" : ObjectId("5274da3e040b61fa15000001"),
"activation" : 1,
"and_or" : "",
"description" : "",
"id" : 13,
"order_id" : 2,
"slug" : [
"apple",
"orange",
"banana"
],
"title" : "Sample"
I take those values from a PHP form and insert them like a record.
The problem is
When I try to remove "orange" from slug, the array breaks and turns into an Object
"slug" : {
"0" : "apple",
"2" : "banana"
},
And I can no longer query it like this, as it returns NULL
db.filters.find({slug: "apple"})
null
I assume that when an array's index is numerically correct, (1, 2, 3, 4) then Mongo will recognize it as an array, otherwise it stores it as an object as custom index is used.
Unfortunately, I cannot change the indexes of the array because they are linked in other documents.
Any ideas?
Thanks!
You are getting null because you might be removing that document using remove method in php.
Instead you need to update slug to array with only apple as its element
db.collection.update($array);
I have this document in the 'families' table:
{
"_id" : {
"$oid" : "51b701a81c1d7bd459000001"
},
"items" : {
"tasks" : [{
"due_date" : "07/06/13",
"assignees" : "Dorel#yahoo.com",
"_id" : {
"$oid" : "51ba0c181c1d7b7c0e00000c"
},
"type" : "task",
"author" : "cornel#gmail.com",
"creation_date" : "13/06/2013",
"name" : "Task0"
}
]
}
}
Using Codeigniter's ActiveRecord Library I'm trying to access an element in the 'tasks' array by doing so ( '$item_id' is the id of the task I'm looking for):
$this->mongo_db->where(array('_id' => $this->session->userdata('family_id')))
->where(array('items.$.$._id' => $item_id));
$query = $this->mongo_db->get('families');
So I tried to use the dot notation and the positional operator. But all I get is an empty array.
Please, help, anyone?
I am trying to match "new york" in a search (not places containing "new" or "york" separately)
here is my current query:
"query" : {
"query_string" : {
"query" : "new york" ,
"fields" : ["city"]
}
},
"filter" : {
"and" : [{
"query" : {
"query_string" : {
"query" : "country:US"
}
}
}]
}
However this keeps returning places named "york" rather than "new york"
I am not fully understanding how this works and would appreciate some help in getting this to actually work for me.
If you want both words to appear in the same document you need to change the default operator like this:
"query" : {
"query_string" : {
"query" : "new york" ,
"fields" : ["city"],
"default_operator" : "AND"
}
}
or specify it ion the query:
"query" : {
"query_string" : {
"query" : "new AND york" ,
"fields" : ["city"]
}
}
Have a look at the query string documentation.
Otherwise, if you want both words to appear close to each other in the same document you need to make a phrase query like this:
"query" : {
"match_phrase" : {
"message" : "new york"
}
}
By default the city field in "analyzed" by Elasticsearch which does the default tokenization of words.
New York => *New*, *York*
In order to keep the tokens intact (mostly used for aggregation), you need to explicitly make the city field "not analyzed" using multi field
'city' => [
'type' => 'string',
'fields' => [
'raw' => [
'type' => 'string',
'index' => 'not_analyzed'
]
]
]
Now you can use city.raw to get non analyzed values.