I am currently working on mongodb for my current project.
{
"_id" : ObjectId("5a168f467cf3661df0df9c11"),
"player_id" : "5a02db1170aaf41013d32747",
"quiz_type" : "Single",
"created_date" : ISODate("2017-11-23T09:05:10Z"),
"questions_answered" : [
{
"question_id" : ObjectId("5a0ac1bfa9897441e038c2f7"),
"player_selection_status" : "Pending",
"time_taken" : 0,
"points_gained" : 0,
"like" : 0
},
{
"question_id" : ObjectId("5a0ac212a9897441e038c2f8"),
"player_selection_status" : "Pending",
"time_taken" : 0,
"points_gained" : 0,
"like" : 0
}
],
"__v" : 0
}
above is my record in player collection, i want to update 2nd of
"questions_answered" : [ {
"question_id" : ObjectId("5a0ac212a9897441e038c2f8"),
"player_selection_status" : "Pending",
"time_taken" : 0,
"points_gained" : 0,
"like" : 0
}
like
"player_selection_status" : "Correct",
"time_taken" : 10,
"points_gained": 5,
"like": 10,
"answered_date":ISODate("2017-11-23T09:05:10Z")
i tried like below
updateData = {questions_answered: {time_taken: 10, like: 1,
answered_date: moment().format()}};
Player_quiz.update({_id: qid, player_id: uid,
"questions_answered.question_id": question_id},
{$set: updateData}).exec();
but it is not worked for me.. please help me out with correct solution ??
You have to use a positional operator $ for array update. Here we have questions_answered as array of documents.
The query to update the document is
db.collection.update(
{"questions_answered.question_id" : ObjectId("5a0ac212a9897441e038c2f8")},
{$set: {
"questions_answered.$.time_taken":10,
"questions_answered.$.player_selection_status" : "Correct",
"questions_answered.$.points_gained": 5,
"questions_answered.$.like": 10,
"questions_answered.$.answered_date":ISODate("2017-11-23T09:05:10Z")
}}
);
db.collection.update({
questions_answered: {
$elemMatch: {
"question_id": ObjectId("5a0ac212a9897441e038c2f8")
}
}
}, {
$set: {
"questions_answered.$.player_selection_status": "Correct",
"questions_answered.$.time_taken": 10,
"questions_answered.$.points_gained": 5,
"questions_answered.$.like": 10,
"questions_answered.$.answered_date": ISODate("2017-11-23T09:05:10Z")
}
}
)
In above mentioned query $elemMatch operator is used to match an element in an array
Related
following is an example of schema I have or trying to query to
{
"_id" : ObjectId("5139f9468efdd10e44000024"),
"name" : "Test Test",
"account_activities" : [
{
"_id" : ObjectId("5139f9468efdd10e44000025"),
"activity_id" : ObjectId("5139f6118efdd10e4400000e"),
"allowance" : 100,
"base_rate" : {
"cents" : 80,
"currency_iso" : "USD"
},
"excess_rate" : {
"cents" : 90,
"currency_iso" : "USD"
},
"quota" : 100,
"taxable" : true,
"description" : "",
"sm" : 1,
"updated_at" : ISODate("2015-07-24T16:18:50.942Z")
}
],
"account_number" : "D-061-5556",
"active" : false,
"balance" : {
"cents" : 0,
"currency_iso" : "USD"
},
}
On running an $unwind I get nothing, is there something I am doing wrong..?
db.accounts.aggregate([{$unwind: "$account_activities"}, {$project: {_id: 0, name: 1, base_rate: 1}}])
I am using mongo shell version 2.6.12
base_rate is still under account_activities. So you have to project it as below
db.accounts.aggregate([
{$unwind: "$account_activities"},
{$project: {
_id: 0,
name: 1,
'account_activities.base_rate': 1
}}
])
I have a collection named donation
{
"_id" : 1,
"reqAmt" : 20,
"filled" : [
{
userId : 1,
depAmt : 5
}
]
},
{
"_id" : 2,
"reqAmt" : 20,
"filled" : []
},
Expected result
{
"_id" : 1,
"reqAmt" : 20,
"filledAmt" : 5
},
{
"_id" : 2,
"reqAmt" : 20,
"filledAmt" : 0
}
I tried with $unwind .but no luck. It always returns
{ "_id" : null, "filledAmt" : 0 }
Below are the codes
db.donation.aggregate([{$unwind:"$filled"},{$project:{"reqAmt":1,"filledAmt":1} },{$group:{"_id":null,"filledAmt":{"$sum":"$filled.depAmt"}}}])
I am working on mongodb for my current project my collection as follows
{
"_id" : ObjectId("5a3a567a8fb6e20f67cb10f7"),
"player_id" : "5a26453db767c01262eddc4e",
"quiz_type" : "Single",
"created_date" : ISODate("2017-12-20T12:24:26Z"),
"questions_answered" : [
{
"question_id" : ObjectId("5a3a0bfc2d53f131068b4567"),
"player_selection_status" : "Wrong",
"time_taken" : 10,
"points_gained" : 0,
"like" : 1,
"answered_date" : "2017-12-20T17:54:30+05:30"
},
{
"question_id" : ObjectId("5a3a0bfc2d53f131068b4568"),
"player_selection_status" : "Correct",
"time_taken" : 10,
"points_gained" : 5,
"like" : 1,
"answered_date" : "2017-12-20T17:54:32+05:30"
},
{
"question_id" : ObjectId("5a3a0bfc2d53f131068b4569"),
"player_selection_status" : "Correct",
"time_taken" : 10,
"points_gained" : 5,
"like" : 1,
"answered_date" : "2017-12-20T17:54:34+05:30"
},
{
"question_id" : ObjectId("5a3a0bfc2d53f131068b456a"),
"player_selection_status" : "Wrong",
"time_taken" : 10,
"points_gained" : 0,
"like" : 1,
"answered_date" : "2017-12-20T17:54:35+05:30"
},
{
"question_id" : ObjectId("5a3a0bfc2d53f131068b456c"),
"player_selection_status" : "Correct",
"time_taken" : 10,
"points_gained" : 5,
"like" : 1,
"answered_date" : "2017-12-20T17:54:37+05:30"
}
],
"__v" : 0
}
I need get data of points_gained : 5 only and my query is
db.player_quiz.find({player_id: "5a26453db767c01262eddc4e", 'questions_answered.points_gained': 5}).pretty()
using above query i am getting all results.. i need only records having questions_answered.points_gained: 5 only
please help me with a solution.
db.player_quiz.aggregate(
// Pipeline
[
// Stage 1
{
$unwind: {
"path": '$questions_answered'
}
},
// Stage 2
{
$match: {
'questions_answered.points_gained': 5
}
},
// Stage 3
{
$group: {
_id: '$_id',
questions_answered: {
$addToSet: '$questions_answered'
},
doc: {
$first: '$$ROOT'
}
}
},
// Stage 4
{
$project: {
questions_answered: 1,
"player_id": '$doc.player_id',
'quiz_type': '$doc.quiz_type',
'created_date': '$doc.created_date'
}
},
]
);
I am getting confused with a simple MongoDB query that I can't figure it out where the problem is. I have a collection like this:
{
"_id" : NumberLong(1939026454),
"username" : "5144269288",
"_type" : 1,
"group_id" : 416,
"user_id" : NumberLong(426661),
"credit_used" : 0.0,
"retry_count" : 1,
"successful" : true,
"type_details" : {
"in_bytes" : 0,
"sub_service_qos" : "",
"sub_service_name" : "rating-group-103",
"out_bytes" : 0,
"sub_service_charging" : "FreeIPTV",
"remote_ip" : ""
},
"logout_time" : ISODate("2017-11-06T07:16:09.000Z"),
"before_credit" : 4560.2962,
"ras_id" : 18,
"caller_id" : "",
"isp_id" : 0,
"duration" : NumberLong(14500),
"details" : {
"connect_info" : "rate-group=103",
"sub_service" : "rating-group-103",
"diameter_request_type" : "initial"
},
"unique_id_value" : "918098048;falcon;Dehghan01000000001415716f9a113697;529;falcon-02b4e8a7__103",
"charge_rule_details" : [
{
"start_in_bytes" : 0,
"charge_rule_id" : 3682,
"start_out_bytes" : 0,
"stop_time" : ISODate("2017-11-06T07:16:09.000Z"),
"stop_out_bytes" : 0,
"start_time" : ISODate("2017-11-06T03:14:29.000Z"),
"charge_rule_desc" : "rating-group-103",
"stop_in_bytes" : 0
}
],
"unique_id" : "acct_session_id",
"login_time" : ISODate("2017-11-06T03:14:29.000Z")
}
I need to filter documents that login_time is between two given dates and type_details.sub_service_name is some string.
I tried this:
db.getCollection('connection_log_partial_data').find({
"type_details" : {"sub_service_name": "rating-group-103"},
"login_time": {
"$gt": ISODate("2016-11-06T03:14:29.000Z"),
"$lt": ISODate("2017-11-06T03:14:29.000Z")
}
});
but it fetches 0 records. Any suggestions?
your query should be something like:
db.getCollection('connection_log_partial_data').find({
"type_details.sub_service_name" : "rating-group-103",
"login_time": {
"$gte": ISODate("2016-11-06T03:14:29.000Z"),
"$lte": ISODate("2017-11-06T03:14:29.000Z")
}
});
In MongoDB, I need to be able to unwind nested an array in a document inside an array inside the main document.
{
"_id" : ObjectId("5808d700536d1a3d69f4cf51"),
"last_name" : "Maity",
"xiith_mark" : 58,
"id" : "3539488",
"first_name" : "Harshavardhan",
"course_name" : "BE/B.Tech",
"institute_name_string" : "Abhayapuri College, P.O. Abhayapuri",
"profile_percentage" : 45,
"xiith_mark_type" : "Percentage",
"xth_mark_type" : "Percentage",
"date_of_birth" : "14-April-1993",
"xth_mark" : 30,
"last_login" : 1470827224,
"percentage" : 55,
"job_details" : [
{
"status" : NumberLong(6),
"applied_date" : NumberLong(1470831441),
"job_id" : NumberLong(92928),
"contact_viwed_status" : 0,
"label_name" : [
"shortlisted",
"rejected"
],
"questionnaire_status" : 0,
"batch_id" : NumberLong(6),
"call_letter" : NumberLong(812)
},
{
"status" : NumberLong(6),
"applied_date" : NumberLong(1470831441),
"job_id" : NumberLong(92928),
"contact_viwed_status" : 0,
"label_name" : [
"shortlisted",
"rejected"
],
"questionnaire_status" : 0,
"batch_id" : NumberLong(6),
"call_letter" : NumberLong(812)
}
],
"branch_name" : "Applied Electronics",
"candidate_state_name" : "West Bengal",
"candidate_city_name_string" : "Kolkata",
"10" : 10,
"12" : 12,
"skills" : "",
"gender" : "Male",
"fw_id" : "FW15884830",
"cgpa" : 0,
"picture_path" : "",
"hq_passout_year" : 2019
}
Based on the record above I need to count the job labels (job_details.label_name).
I have tried the following query:
db.response.aggregate(
{"$match":type_match},
{"$unwind": "$job_details" },
{"$group":
{
"_id":"$job_details.label_name",
"count": {"$sum": 1 }
}
}
])
The output is:
{
"count": 2,
"_id": [
"shortlisted",
"rejected"
]
}
But I want the output to be:
[
{
"count": 1,
"_id": "shortlisted"
},
{
"count": 1,
"_id": "rejected"
}
]
How can I get this output?
In unwind stage, field should be an array field. If not array field, it treats it as array of 1 element.
From the docs:
Changed in version 3.2: $unwind stage no longer errors on non-array operands. If the operand does not resolve to an array but is not missing, null, or an empty array, $unwind treats the operand as a single element array.
Answer to your query:
db.response.aggregate([
{
$project:
{
"job_details.label_name":1,
_id:0
}
},
{
$unwind:"$job_details.label_name"
},
{
$group:
{
_id:"$job_details.label_name",
count:{$sum:1}
}
}
])
Refer Shell Output