My Mongo collection document is as:
Array
(
[_id] => MongoId Object
(
[$id] => 52ddf885f786208bf58020df
)
[FirstName] => Aloma
[State] => AR
[Title] => AVP
[Zip] => 71953
[campaign_id] => Array
(
[0] => 52fba54fce798c441400002b
[1] => 52fba687ce798c441400002c
)
)
campaign_id is the array of mongoid's reference to other collection.
How can I get the documents with campaign_id = 52fba54fce798c441400002b
Here we have to search mongoid 52fba54fce798c441400002b in array of campaign_id.
You can use following code in mongoDB.
db.collection.find(campaign_id:[52fba54fce798c441400002b])
It will return the documents that contains [52fba54fce798c441400002b] as campaign_id value exactly.
For example :
{ "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }
{ "_id" : ObjectId("52fc4b8d633d0c54e662fc74"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 12, 14 ] }
{ "_id" : ObjectId("52fc4b86633d0c54e662fc73"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12, 14 ] }
{ "_id" : ObjectId("52fc4b7d633d0c54e662fc72"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12 ] }
> db.scores.find({campaign_id:[11]})
{ "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }
Same thing, you can implement in PHP using php mongo client.
I hope it solves your problem.
Related
I am facing an issue while pushing the data in nested document using mongodb and laravel. The collection structure are as follows:
{
"_id" : ObjectId("56f432f5b720531878f8c935"),
"test1" : [{
"Name" : "test",
"_id" : ObjectId("56f432f5b720531878f85926"),
"isActive" : 0,
"test2" : [{
"Name" : "testing",
"_id" : ObjectId("56f432f5b720531858f8c965"),
"isActive" : 0,
"test3" : [{
"Name" : "test",
"_id" : ObjectId("56f432f5b720541858f8c965"),
"isActive" : 0
}, {
"Name" : "test1",
"_id" : ObjectId("56ffeb9c7ae65b1124000714")
}]
}]
}],
"created" : ISODate("2012-12-31T18:30:00Z")
}
I tried to solve the issue using aggregator but stood still in finding the index of the nested document. since i can push the data using
test1.0.test2.test3 likewise... in there any way to find the index of the nested document using mongodb alone?
array('$unwind' => '$test1'),
array('$unwind' => '$test1._id'),
array('$unwind' => '$test1.test2'),
array('$unwind' => '$test1.test2._id'),
array(
'$match' => array(
'$and' => array(
array('test1._id' => array('$in' => array('234234234'))),
array('test1.test2._id' => array('$in' => array('456456456'))),
)
)
),
array('$project' => array(
'test1' => 1,
)),
My aim is to create inner array to main array when search for userid as 1
Below is my data
{ "_id" : 2,
"name" : "test1",
"data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},
{"_id" : "2","file" : "nic1","userid" : [1 ] },
{"_id" : 3,"file" : "nick2","userid" : [1,2 ]}
]},
{ "_id" : 3,
"name" : "test2",
"data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ] },
{"_id" : "2","file" : "nic1", "userid" : [3,2 ] }
]}
need to get out put as
{"_id" : 1,"file" : "nic", "userid" : [1,2 ],"main_name" : "test1","main_id" : 2},
{"_id" : 2,"file" : "nic1","userid" : [1 ] ,"main_name" : "test1","main_id" : 2 },
{"_id" : 3,"file" : "nick2","userid" : [1,2 ],"main_name" : "test2","main_id" : 3},
{"_id" : 1,"file" : "nic","userid" : [1,2 ] ,"main_name" : "test2" ,"main_id" : 3}
Basically the same answer to your last question, but without the $group to reconstruct the array and use a $project instead to restructure the document from the already de-normalized array elements.
$collection->aggregate(array(
array( '$match' => array( "data.userid" => 1 )),
array( '$unwind' => '$data' ),
array( '$match' => array( 'data.userid' => 1 )),
array(
'$project' => array(
'_id' => '$data._id',
'nic' => '$data.nic',
'user_id' => '$data.user_id',
'main_name' => '$name',
'main_id' => '$_id'
)
)
))
I'm having Mongo structure like this,
"campaigns":[{
"summary" :
[ {
"postcode" : [ {
"id" : "71",
"name" : "Recall 1",
"qty" : 3,
"history" :
[ { "timestamp" : "2014-12-16 11:15:32",
{ "timestamp": "2014-12-16 11:15:53"
} ]
} ]
},
{
"postcode" :
[ {
"id" : "72",
"name" : "Recall 2",
"qty" : 1,
"history" : [ { "timestamp" : "2014-12-16 11:15:53" } ]
} ]
}]
I'm trying to i) increment qty of postcode.id : 72 ii) insert another timestamp for that same postcode id 72.
My code:
$collection->update(array("campaigns.task.id" => $b,"_id"=> new MongoId($objectid),"campaigns.0.summary.0.postcode.0.id" => $a), array('$inc' => array('campaigns.0.summary.0.postcode.0.qty' => 1)));
$collection->update(array("campaigns.task.id" => $b,"_id"=>new MongoId($objectid),"campaigns.0.summary.0.postcode.0.id" => $a),
array('$addToSet' => array('campaigns.0.summary.0.postcode.0.history' => array("timestamp"=>$now->format('Y-m-d H:i:s')))));
but postcode.id = 72 not gets updated, i'm confused with this nested subdocument can anyone suggest me the solution ?
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I calling a url which returns the data as json format. I like pick out some variables from this output. The ouput is as follows:
{
"graph_property" : [ {
"name" : "calculation_method",
"value" : "Geo Mean"
}, {
"name" : "graph_type",
"value" : "TIME"
} ],
"measurement" : [ {
"id" : "1132282",
"alias" : "example.com"
"bucket_data" : [ {
"name" : "2013-JUN-20 11:23 AM",
"id" : 1,
"perf_data" : {
"value" : "4.878",
"unit" : "seconds"
},
"avail_data" : {
"value" : "100.00",
"unit" : "percent"
},
"data_count" : {
"value" : "1",
"unit" : "#"
}
}, {
"name" : "2013-JUN-20 11:28 AM",
"id" : 2,
"perf_data" : {
"value" : "-",
"unit" : "seconds"
},
"avail_data" : {
"value" : "-",
"unit" : "percent"
},
"data_count" : {
"value" : "-",
"unit" : "#"
}
}, {
"name" : "2013-JUN-20 11:33 AM",
"id" : 3,
"perf_data" : {
"value" : "-",
"unit" : "seconds"
},
"avail_data" : {
"value" : "-",
"unit" : "percent"
},
"data_count" : {
"value" : "-",
"unit" : "#"
}
} ],
"graph_option" : [ {
"name" : "perfwarning",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "perfcritical",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "availwarning",
"value" : "-",
"unit" : "percent"
}, {
"name" : "availcritical",
"value" : "-",
"unit" : "percent"
}, {
"name" : "bucketsize",
"value" : "300",
"unit" : "seconds"
}, {
"name" : "rows",
"value" : "3",
"unit" : "#"
}, {
"name" : "pagecomponent",
"value" : "User Time",
"unit" : "seconds"
}, {
"name" : "avg_perf",
"value" : "4.878",
"unit" : "seconds"
}, {
"name" : "avg_avail",
"value" : "100.00",
"unit" : "percent"
}, {
"name" : "total_datapoint_count",
"value" : "1",
"unit" : "#"
}, {
} ]
} ],
"link" : {
"type" : "application/json",
"href" : "http://api.keynote.com/",
"rel" : "slotmetadata"
}
}
The values I am interested are these:
"
name" : "2013-JUN-20 11:23 AM",
"value" : "4.878",
"name" : "2013-JUN-20 11:28 AM",
"value" : "-",
"name" : "2013-JUN-20 11:33 AM",
"value" : "-",
The reason I am doing it this way is that sometimes, the web service call returns emtpy values. From this output, I like to pick the latest date that does not have an empty value and then print out the name and value which is not empty.
Is there an easy way to pick the name and value field from this json output?
when I do
$data<-json_decode($resp)
print_r($data)
one portion of the data is this:
[measurement] => Array
(
[0] => Array
(
[id] => 1132282
[alias] =>example.com
[bucket_data] => Array
(
[0] => Array
(
[name] => 2013-JUN-20 01:23 PM
[id] => 1
[perf_data] => Array
(
[value] => 3.074
[unit] => seconds
)
[avail_data] => Array
(
[value] => 100.00
[unit] => percent
)
[data_count] => Array
(
[value] => 1
[unit] => #
)
)
[1] => Array
(
[name] => 2013-JUN-20 01:28 PM
[id] => 2
[perf_data] => Array
(
[value] => 3.416
[unit] => seconds
)
[avail_data] => Array
(
[value] => 100.00
[unit] => percent
)
[data_count] => Array
(
[value] => 1
[unit] => #
)
)
[2] => Array
(
[name] => 2013-JUN-20 01:33 PM
[id] => 3
[perf_data] => Array
(
[value] => -
[unit] => seconds
)
[avail_data] => Array
(
[value] => -
[unit] => percent
)
[data_count] => Array
(
[value] => -
[unit] => #
)
)
)
use built-in json_decode function
$data = json_decode('your_json_here'); //$data will be "stdClass" object
$data = json_decode('your_json_here', true); //$data will be "array" object
then you can operate with $data as with regular array/object
if type = stdClass:
$data->measurement[0]->bucket_data[0]->name; // will be equal "2013-JUN-20 11:23 AM"
if type = array
$data['measurement'][0]['bucket_data'][0]['name']; // will be equal "2013-JUN-20 11:23 AM"
I have followed model stored in mongoDB:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
}]
}
I try to find criteria to update 2nd element (id=2) aka add new String.
"new_element" : "foo"
So new view should be:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
"new_element" : "foo"
}]
}
Form PHP
When I try to find 2nd node by id I use:
$array = $collection_bios2->findOne(
array("_id" => "some_table_name", "content.id" => "2"),
array("_id" => 0, "content.$" => 1)
);
But when I try to update it, new node enters under content:
$newdata = array('$set' => array("new_element" => "foo"));
$collection_bios2->update(
array("_id" => "some_table_name", "content.id" => "2"),
$newdata
);
I get:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
}],
"new_element" : "foo"
}
Whats wrong in my implementation?
Please, help,
Maxim
You need to use the positional operator here:
array('$set'=>array('content.$.new_element':'foo'))
You can read more about it here: http://docs.mongodb.org/manual/reference/operator/positional/