I have one collection in mongodb and my document is :
"_id" : ObjectId("55caf368b649fe6d068b4568"),
"title" : "lrgwkwjg",
"comments" : [
{
"id" : NumberLong(793644433634148),
"name" : "werkwehgbwejhf",
"title" : "jrg nejrngjern gjer",
"created_at" : {
"date" : "2015-08-15 11:01:54",
"timezone_type" : NumberLong(3),
"timezone" : "UTC"
},
"comment" : "nrjfnwejf wjkenf kjwef",
"user_id" : ObjectId("5599009fb649fe72188b4567"),
"approve" : false,
"url" : "http://takhfif.dev/api/v1/comment/793644433634148"
},
{
"id" : NumberLong(966385963890287),
"name" : "werkwehgbwejhf",
"title" : "jrg nejrngjern gjer",
"created_at" : {
"date" : "2015-08-15 11:09:17",
"timezone_type" : NumberLong(3),
"timezone" : "UTC"
},
"comment" : "nrjfnwejf wjkenf kjwef",
"user_id" : ObjectId("5599009fb649fe72188b4567"),
"approve" : false,
"url" : "http://takhfif.dev/api/v1/comment/966385963890287"
}
],
I want to update comments as record id = 966385963890287, and the approve field must be convert to true - in conclude, i use this query as below:
public function approveComment($id,Product $comment)
{
$comment->update([
'comments.id'=>$id
]
,
[
'$set'=>[
'comments.$.approve'=>true
]
]
);
}
But if i use that query i got an error as below :
MongoWriteConcernException in Collection.php line 42:
localhost:27017: cannot use the part (comments of comments.id) to traverse the element ({comments: [ { id: 793644433634148, name: "werkwehgbwejhf", title: "jrg nejrngjern gjer", created_at: { date: "2015-08-15 11:01:54", timezone_type: 3, timezone: "UTC" }, comment: "nrjfnwejf wjkenf kjwef", user_id: ObjectId('5599009fb649fe72188b4567'), approve: true, url: "http://takhfif.dev/api/v1/comment/793644433634148" }, { id: 966385963890287, name: "werkwehgbwejhf", title: "jrg nejrngjern gjer", created_at: { date: "2015-08-15 11:09:17", timezone_type: 3, timezone: "UTC" }, comment: "nrjfnwejf wjkenf kjwef", user_id: ObjectId('5599009fb649fe72188b4567'), approve: false, url: "http://takhfif.dev/api/v1/comment/966385963890287" } ]})
How can i fix that?
Hint : i use jenssegers/laravel-mongodb package in laravel5.1
Related
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);
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'm trying to create a mongo query which will return results where all arrays have a specific element set to false.
An example data record :-
images: [
{
id: ObjectId("516bef7fc05e877b31000000"),
primary: true
},
{
id: ObjectId("516bef2ac05e879622000000"),
primary: false
},
{
id: ObjectId("516beeb7c05e879e2a000000"),
primary: false
}
],
name: "test",
etc: "etc"
I only wish to find documents where all primary fields are set to false however normally (using no query selectors or elemMatch) mongo will return this document because at least 1 of the array elements match.
How would I make mongo only return documents where they all match my search parameters?
Many thanks.
You can do this with the aggregation framework quite easily:
db.so.aggregate( [
{ $unwind: "$images" },
{ $group: {
_id: '$_id',
all: { $sum: 1 },
all_primary: { $sum: { $cond: [ { $eq: [ '$images.primary', true ] }, 1, 0 ] } },
images: { $push: '$images' },
name: { $first: '$name' },
etc: { $first: '$etc' },
} },
{ $project: {
_id: 1,
images: 1,
name: 1,
etc: 1,
same: { $cond: [ { $eq: [ '$all', '$all_primary' ] }, 1, 0 ] }
} },
{ $match: { 'same' : 1 } }
] );
With this as input:
{
"_id" : ObjectId("5203730bf8eaa52a846ebc3e"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000010"),
"primary" : true
}
],
"name" : "Derick",
"Etc" : true
}
{
"_id" : ObjectId("52037315f8eaa52a846ebc3f"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : false
}
],
"name" : "James",
"Etc" : true
}
{
"_id" : ObjectId("520373621a78238235b6ffbf"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : false
}
],
"name" : "James",
"etc" : true
}
{
"_id" : ObjectId("5203736b1a78238235b6ffc0"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : true
}
],
"name" : "James",
"etc" : true
}
This outputs:
{
"result" : [
{
"_id" : ObjectId("5203736b1a78238235b6ffc0"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : true
}
],
"name" : "James",
"etc" : true,
"same" : 1
},
{
"_id" : ObjectId("5203730bf8eaa52a846ebc3e"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000010"),
"primary" : true
}
],
"name" : "Derick",
"etc" : null,
"same" : 1
}
],
"ok" : 1
}
Wouldn't it be much simpler to exclude all documents where images has primary:true element?
{ "images" :
{ "$not" :
{"$elemMatch" : { "primary" : true }}
}
}
Naturally, this is only applicable to a boolean nested field, as in this case.
Having sort trouble with a similar kind of query...But just look at Dericks code, the docs say not to use $first within a $group operation, unless the $group operation is immediately after a $sort.
http://docs.mongodb.org/manual/reference/operator/aggregation/first/#grp._S_first
Presuming Images is a document you could use(from the shell):
db.images.find({'primary':'false'})
If Images is an object:
db.mydoc.find({'images':{'primary':'false'}})
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