My collections are like this:
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : "10000",
"_score" : 1.0,
"_source" : {
"user_id" : 12,
"index_date" : {
"date" : "2018-02-06 14:25:49.816952",
"timezone_type" : 3,
"timezone" : "UTC"
},
"rating" : null,
"orders" : [
{
"hour" : "08",
"count" : 1
},
{
"hour" : "10",
"count" : 1
}
],
"products" : [
{
"p_id" : 970111,
"count" : 4
},
{
"p_id" : 1280811,
"count" : 1
},
]
}
},
and tried to access to {"hour":"10"}
My query is:
$query = new Query\Nested();
$query->setPath('orders');
$term = new Term();
$term->setTerm('orders.hour', $order->getCreatedAt()->format('H'));
$query->setQuery($term);
dump($finder->find($query));die;
but i got the following error:
[Elastica\Exception\ResponseException]
failed to create query: {
"nested" : {
"query" : {
"term" : {
"orders.hour" : {
"value" : "12",
"boost" : 1.0
}
}
},
"path" : "orders",
"ignore_unmapped" : false,
"score_mode" : "avg",
"boost" : 1.0
}
} [index: test_index] [reason: all shards failed]
Your documents not look like nested queries.
I assume that finder is your repository manager that is defined as orders repository, your code should look something like this
$finder = $this->get('fos_elastica.repository_manager')->getRepository('YourBundle:order');
$boolquery = new Query\BoolQuery();
$term = new Query\Term();
$term->setTerm('hour', $order->getCreatedAt()->format('H'));
$boolquery->addMust($term);
$finder->find($boolquery);
Related
when trying to "JOIN" operation with $lookup but results count is ok but "as" document is empty
I have two collections and i need to get user details from subscribercol with user_id in employer_jobscol
subscribercol
{
"_id" : ObjectId("58187e7551d244640626d7e1"),
"type" : "job_seeker",
"firstname" : "vishnu",
"lastname" : "kumar pv",
"email_array" : {
"primary" : "test#test.com",
"secondary" : "test#test.test",
"verified" : false
},
"address_array" : {
"address" : "test address22d",
"streetname" : "test222d",
"pincode" : "test222d",
"city" : "dddd",
"state" : "ALASKA2d",
"country" : "Argentinad"
},
"phone_array" : {
"primary" : "",
"secondary" : "",
"verified" : ""
},
"languages" : [
"english",
"malayalam",
"english2"
]
}
employer_jobscol
{
"_id" : ObjectId("582ada6b51d244073e2a7541"),
"employer_id" : ObjectId("58187e7551d244640626d7e1"),
"job_id" : "testjob16946",
"job_title" : "Test Job 25",
"category" : "IT",
"vacancies" : "5",
"salary" : "200000",
"location" : "Kollam",
"employer_name" : "test test",
"mobile" : "9123456987",
"video" : "",
"image" : "",
"work_place" : "option1",
"email" : "test#test.test",
"skills" : [
"php"
],
"isActive" : true,
"applied_users" : [
{
"user_id" : ObjectId("581b364751d2445c311cf6f1"),
"accepted" : false
},
{
"user_id" : ObjectId("58187e7551d244640626d7e1"),
"accepted" : false
}
]
}
my database query here, (executed with Robomongo )
db.getCollection('employer_jobscol').aggregate([ {
$unwind: "$applied_users"
},
{
$lookup:
{
from: "subscribercol",
localField: "user_id",
foreignField: "_id",
as: "subscribercol_docs"
}
}
])
Result is
{
"_id" : ObjectId("582ada6b51d244073e2a7541"),
"employer_id" : ObjectId("58187e7551d244640626d7e1"),
"job_id" : "testjob16946",
"job_title" : "Test Job 25",
"category" : "IT",
"vacancies" : "5",
"salary" : "200000",
"location" : "Kollam",
"employer_name" : "test test",
"mobile" : "9123456987",
"video" : "",
"image" : "",
"work_place" : "option1",
"email" : "test#test.test",
"skills" : [
"php"
],
"isActive" : true,
"applied_users" : {
"user_id" : ObjectId("58187e7551d244640626d7e1"),
"accepted" : false
},
"subscribercol_docs" : []
}
here subscribercol_docs is empty array i need user info (name, address etc..),
Because there is no user_id field in local document its "applied_users.user_id"
Try this
db.getCollection('employer_jobscol').aggregate([ {
$unwind: "$applied_users"
},
{
$lookup:
{
from: "subscribercol",
localField: "applied_users.user_id", // <-- check here
foreignField: "_id",
as: "subscribercol_docs"
}
}
])
I am a newbie in MONGODB.
I need to get the count of entries for each DEPLOYED date till today using MAPREDUCE function. i tired to look for answers but none matched according to my needs. Please Help !
I TRIED THIS BUT DIDNT WORK.
How to get the count of records on each date in mongodb?
Even tired many others but still in vain.
I am trying to write a php code for this one but even if u help me just by mongodb i can do the rest. Thanks in Advance.
P.s. - i read about getting date from _id and i tried it but still in vain.
My Mongodb Structure
{
"_id" : ObjectId("57e90143fe649e701e156e8d"),
"description" : "log_details",
"servicelog" : [
{
"_id" : ObjectId("57e901436803fa2125f7b82a"),
"name" : "Sam",
"gender" : "Male",
"telephone" : "1234567543",
"pincode" : "1234",
"servicetype" : "ambulance",
"assigned_vehiclenumber" : "MH12AA0001",
"Deploy_Timestamp" : {
"date" : "2016-09-26 16:36:43",
"timezone_type" : 3,
"timezone" : "Asia/Calcutta"
},
"Release_Timestamp" : {
"date" : "2016-09-26 16:51:26",
"timezone_type" : 3,
"timezone" : "Asia/Calcutta"
}
},
{
"_id" : ObjectId("57e904cd6803fa2125f7b82b"),
"name" : "Ram",
"gender" : "Male",
"telephone" : "6543134",
"pincode" : "411011",
"servicetype" : "ambulance",
"assigned_vehiclenumber" : "MH12AA0001",
"Deploy_Timestamp" : {
"date" : "2016-09-26 16:51:49",
"timezone_type" : 3,
"timezone" : "Asia/Calcutta"
},
"Release_Timestamp" : {
"date" : "2016-09-26 17:00:19",
"timezone_type" : 3,
"timezone" : "Asia/Calcutta"
}
},
{
"_id" : ObjectId("57e90e9a6803fa6879f7b82d"),
"name" : "Atharva",
"gender" : "Male",
"telephone" : "12343143",
"pincode" : "34532",
"servicetype" : "ambulance",
"assigned_vehiclenumber" : "MH12AA0001",
"Deploy_Timestamp" : {
"date" : "2016-09-26 17:33:38",
"timezone_type" : 3,
"timezone" : "Asia/Calcutta"
},
"Release_Timestamp" : {
"date" : "2016-09-26 18:28:39",
"timezone_type" : 3,
"timezone" : "Asia/Calcutta"
}
},
.....
}
I have an Mongo collection containing ~7 millions events. To get the events that happend for an aggregate I have the following PHP code
$client = new MongoClient();
$db = $client->selectDB('db_name');
$collection = $db->selectCollection('events');
foreach($collection->find([
'headers.for' => '89d115f8-0b2f-470e-9495-2a07d9dfb942',
])->sort([
'headers.occurredOn' => 1,
'_id' => 1,
]) as $event) {
var_dump($event);
}
When I run the above PHP code I get an MongoCursorTimeoutException after 30 seconds.
But when I run the same code without a sort on _id, so:
$client = new MongoClient();
$db = $client->selectDB('db_name');
$collection = $db->selectCollection('events');
foreach($collection->find([
'headers.for' => '89d115f8-0b2f-470e-9495-2a07d9dfb942',
])->sort([
'headers.occurredOn' => 1,
]) as $event) {
var_dump($event);
}
The error does not occur and get instant results (which is one record).
So why does a MongoCursorTimeoutException occur when a sort on _id is added?
The indexes for the collection looks as follow
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db.events"
},
{
"v" : 1,
"key" : {
"headers.occurredOn" : NumberLong(1),
"_id" : NumberLong(1)
},
"name" : "headers_occurredOn_1__id_1",
"ns" : "db.events"
},
{
"v" : 1,
"key" : {
"headers.for" : NumberLong(1)
},
"name" : "headers_for_1",
"ns" : "db.events"
}
]
Well the problem was the indexes I had.
{
"v" : 1,
"key" : {
"headers.occurredOn" : NumberLong(1),
"_id" : NumberLong(1)
},
"name" : "headers_occurredOn_1__id_1",
"ns" : "db.events"
}
After I dropped this one and added the following:
{
"v" : 1,
"key" : {
"headers.occurredOn" : NumberLong(1)
},
"name" : "headers.occurredOn_1",
"ns" : "db.events"
}
Everything went smooth
So my collection looks like this:
{
"_id" : ObjectId("52722429d874590c15000029"),
"name" : "Bags",
"products" : [{
"_id" : ObjectId("527225b5d87459b802000029"),
"name" : "Prada",
"description" : "Prada Bag",
"points" : "234",
"validDate" : 1382562000,
"link" : "dasdad",
"code" : "423423424",
"image" : null
}, {
"_id" : ObjectId("5272307ad87459401a00002a"),
"name" : "Gucci",
"description" : "Gucii bag",
"points" : "2342",
"validDate" : 1383170400,
"link" : "dsadada",
"code" : "2342",
"image" : null
}]
}
and I want to get only the product with the _id 527225b5d87459b802000029, I tried this:
$this->find(array(
'_id' => new \MongoId('52722429d874590c15000029'),
'products._id' => new \MongoId('527225b5d87459b802000029')
));
But it returns the entire array for that collection, and I only want one...can this be done in mongo?
As mentioned in comments, you have to add a projection, and more precisely an $elemMatch. No need to use the aggregation framework in that case.
Example :
find( { _id: 1, "products._id": 4 }, { products: { $elemMatch: { _id: 4 } } } ).pretty()
How to search value in multidimensional array,
for example I want to search example keyword in the following data in mongodb
I used to fetch all data from command
>db.info.find()
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 3,
"name" : "XYZ",
"email" : "xyz#demo.com"
},
{
"sno" : 4,
"name" : "ABC",
"email" : "abc#demo.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
Now, to find data having example I used command
>db.info.find({"info.email":"example"})
and it gives
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 3,
"name" : "XYZ",
"email" : "xyz#demo.com"
},
{
"sno" : 4,
"name" : "ABC",
"email" : "abc#demo.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
But I want only 3 out of 5 sub rows like
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
Rohan, MongoDB always returns the whole document that you are searching on. You can't just make it return the array elements in which your keyword was found. If you want to do that, then you need to make sure all all embedded documents in the "info" field are in their own collection. And that might mean that you need to link them back to the original document in your "info" collection. Perhaps something like:
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
"info_id" : "12345",
},
Alternatively, you can of course do post-processing in PHP to obtain only the rows that you want.
Perhaps this is a good idea?
http://php.net/manual/en/class.mongoregex.php
I tried Map Reduce Function and it works on this type of problems the code is something like that:
Write a map function
map=function ()
{
filter = [];
this.info.forEach(function (s) {if (/example/.test(s.email)) {filter.push(s);}});
emit(this._id, {info:filter});
}
Write a reduce function
reduce=function(key, values) { return values;}
MapReduce Function
res=db.info.mapReduce(map,reduce,{out:{inline:1}})
And The Output look likes:
"results" : [
{
"_id" : ObjectId("4f9a2de0ea4a65c3ab85a9d3"),
"value" : {
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
}
],
"timeMillis" : 1,
"counts" : {
"input" : 3,
"emit" : 3,
"reduce" : 0,
"output" : 3
},
"ok" : 1,
Now you can find your search data from
printjson(res.results)
Did you try $ (projection)?
db.info.find({"info.email":"example"}, {"info.email.$":1})
document