How to avoid duplicate values ? [duplicate] - mongodb

This question already has answers here:
Return document in each group with max value using MongoDB
(2 answers)
Closed 4 years ago.
I am new to Mongo Db and would appreciate some help with this query.my mongodb data transection same ids number of thre based on create_date how to display each transection id first record
db.collection.aggregate([{
{"$project": {
"RESOURCE_ID": 1,
"TRANSACTION_ID":1,
"CREATE_DATE":1
}}
])
output:
RESOURCE_ID TRANSACTION_ID CREATE_DATE
1 "100-101" "0:ffff0a0a0983UY:-142" "Fri Sep 07 16:51:30IST2018"
2 "100-101" "0:ffff0a0a0983UY:-142" "Fri Sep 07 16:51:29IST2018"
3 "100-101" "0:ffff0a0a0983UY:-142" "Fri Sep 07 16:51:29IST2018"
4 "100-102" "0:ffff0a0a0983UY:-111" "Fri Sep 06 16:51:29IST2018"
5 "100-102" "0:ffff0a0a0983UY:-111" "Fri Sep 06 16:51:28IST2018"
expected output:
RESOURCE_ID TRANSACTION_ID CREATE_DATE
1 "100-101" "0:ffff0a0a0983UY:-142" "Fri Sep 07 16:51:30IST2018"
2 "100-102" "0:ffff0a0a0983UY:-111" "Fri Sep 06 16:51:29IST2018"

This is what you want:
db.collection.aggregate([{"$sort": {CREATE_DATE: 1}},{$group:{"_id": "$RESOURCE_ID", TRANSACTION_ID: {$first: "$TRANSACTION_ID"}, "CREATE_DATE": {$first: "$CREATE_DATE"}}}])
What it does - sorts all the documents by the date, in an ascending order, because we want the oldest document first (notice the sort clause).
Then, it groups the documents by the field "RESOURCE_ID" (which is marked as the new _id), and takes the $first TRANSACTION_ID and $first CREATE_DATE.
Notice that you will have to convert your timestamps to actual timestamps, in order for mongo to really understand the order of the timestamps. Otherwise mongo will sort them as strings, and this isn't what you want
An example:
> db.collection.find().pretty()
{
"_id" : ObjectId("5bd2bf353ca22147747ec212"),
"RESOURCE_ID" : "100-101",
"TRANSACTION_ID" : "0:ffff0a0a0983UY:-142",
"CREATE_DATE" : ISODate("2017-10-13T10:53:53Z")
}
{
"_id" : ObjectId("5bd2bf3c3ca22147747ec213"),
"RESOURCE_ID" : "100-101",
"TRANSACTION_ID" : "0:ffff0a0a0983UY:-142",
"CREATE_DATE" : ISODate("2017-10-14T10:53:53Z")
}
{
"_id" : ObjectId("5bd2bf3c3ca22147747ec214"),
"RESOURCE_ID" : "100-102",
"TRANSACTION_ID" : "0:ffff0a0a0983UY:-111",
"CREATE_DATE" : ISODate("2017-10-13T10:53:53Z")
}
{
"_id" : ObjectId("5bd2bf3c3ca22147747ec215"),
"RESOURCE_ID" : "100-102",
"TRANSACTION_ID" : "0:ffff0a0a0983UY:-111",
"CREATE_DATE" : ISODate("2017-10-14T10:53:53Z")
}
> db.collection.aggregate([{"$sort": {CREATE_DATE: 1}},{$group:{"_id": "$RESOURCE_ID", TRANSACTION_ID: {$first: "$TRANSACTION_ID"}, "CREATE_DATE": {$first: "$CREATE_DATE"}}}])
{ "_id" : "100-102", "TRANSACTION_ID" : "0:ffff0a0a0983UY:-111", "CREATE_DATE" : ISODate("2017-10-13T10:53:53Z")
{ "_id" : "100-101", "TRANSACTION_ID" : "0:ffff0a0a0983UY:-142", "CREATE_DATE" : ISODate("2017-10-13T10:53:53Z")
Also, worth mentioning that you should add indexes for CREATE_DATE because you are sorting by this field, and to RESOURCE_ID because mongo has to sort it in order to group by it

Related

Date query in mongodb isn't working

Type of document
{
"_id" : ObjectId("585232c2bbdfc4243ecf2670"),
"field1" : "value1",
"date" : "Mon Dec 19 2016 14:45:17 GMT+0530 (IST)",
"field2" : "value2",
"field3" : true
}
Query used:
db.myCollection.find({"date":{"$lt":new Date()}})
I want to run this query on 12:05 AM to fetch all past records upto
yesterday 23:59:59
It seems that the value is a string and not a Date.
fields of type Date should appear like this:
"date" : ISODate("2016-12-19T14:45:17.000Z");
and not like what you're seeing.
Make sure you save a Date object into the collection, and not a string representation of it.

Querying a list in MongoDB

I have a document that looks like this:
{
"_id" : ObjectId("570fc2381d4899be8a8ec9d9"),
"statuses" : [
{
"created_at" : "Wed Apr 13 09:56:39 +0000 2016",
"id" : 7.20188946337153e+017,
"id_str" : "720188946337153024",
"text" : "RT #BCC_Assicura: #FormulaAuto la #polizza #Auto e #Moto economica BccPordenonese - #BCC #Assicurazioni #Click2go"
},
{
"created_at" : "Wed Apr 13 09:40:13 +0000 2016",
"id" : 7.20184809658708e+017,
"id_str" : "720184809658707970",
"text" : "Auto e moto storiche, vademecum su assicurazione e bollo - \n#autostoriche #bollo #RCauto #ASI #FMI"
}
]}
How do I query for all the records where the variable text contains the string "assicur"?
Thank you!
One possibility would be to use a regex;
> db.test.find({"statuses.text":{$regex: 'assicur'}})
That said, this will not be possible to index in mongodb, so it's probably best done along with other filters that cut the documents down to a small set before doing the string matching.

mongo query select only first of month

is it possible to query only the first (or last or any single?) day of the month of a mongo date field.
i use the $date aggregation operators regularly but within a $group clause.
basically i have field that is already aggregated (averaged) for each day of the month. i want to select only one of these days (with the value as a representative of the entire month.)
following is a sample of a record set from jan 1, 2014 to feb 1, 2015 with price as the daily price and 28day_avg as the trailing monthly average for 28 days.
{ "date" : ISODate("2014-01-01T00:00:00Z"), "_id" : ObjectId("533b3697574e2fd08f431cff"), "price": 59.23, "28day_avg": 54.21}
{ "date" : ISODate("2014-01-02T00:00:00Z"), "_id" : ObjectId("533b3697574e2fd08f431cff"), "price": 58.75, "28day_avg": 54.15}
...
{ "date" : ISODate("2015-02-01T00:00:00Z"), "_id" : ObjectId("533b3697574e2fd08f431cff"), "price": 123.50, "28day_avg": 122.25}
method 1.
im currently running an aggregation using $month data (and summing the price) but one issue is im seeking to retrieve the underlying date value ISODate("2015-02-01T00:00:00Z") versus the 0,1,2 value that comes with several of the date aggregations (that loop at the first of the week, month, year). mod(28) on a date?
method 2
i'd like to simply pluck out a single record of the 28day_avg as representative of the period. the 1st of the month would be adequate
the desired output is...
_id: ISODate("2015-02-01T00:00:00Z"), value: 122.25,
_id: ISODate("2015-01-01T00:00:00Z"), value: 120.78,
_id: ISODate("2014-12-01T00:00:00Z"), value: 118.71,
...
_id: ISODate("2014-01-01T00:00:00Z"), value: 53.21,
of course, the value will vary from method 1 to method 2 but that is fine. one is 28 days trailing while the other will account for 28, 30, 31 day months...dont care about that so much.
A non-agg is ok but also doesnt work. aka {"date": { "$mod": [ 28, 0 ]} }
To pick the first of the month for each month (method 2), use the following aggregation:
db.test.aggregate([
{ "$project" : { "_id" : "$date", "day" : { "$dayOfMonth" : "$date" }, "28day_avg" : 1 } },
{ "$match" : { "day" : 1 } }
])
You can't use an index for the match, so this is not efficient. I'd suggest adding another field to each document that holds the $dayOfMonth value, so you can index it and do a simple find:
{
"date" : ISODate("2014-01-01T00:00:00Z"),
"price" : 59.23,
"28day_avg" : 54.21,
"dayOfMonth" : 1
}
db.test.ensureIndex({ "dayOfMonth" : 1 })
db.test.find({ "dayOfMonth" : 1 }, { "_id" : 0, "date" : 1, "28day_avg" : 1 })

mongodb pull all element from the array

Suppose I have the following schema:
"_id" : 1,
"n" : [{
"a" : ObjectId("4ef0ca414653b7c866040000"),
"d" : new Date("Thu, 22 Dec 2011 04:53:56 GMT +04:00")
}, {
"a" : ObjectId("4ef0ca414653b9c866040000"),
"d" : new Date("Thu, 22 Dec 2011 04:54:11 GMT +04:00")
}, {
"a" : ObjectId("4ef0ca424653b9c866040000"),
"d" : new Date("Thu, 22 Dec 2011 04:54:30 GMT +04:00"),
}]
and I need to remove all n, where d is less than specific date.
So I thought I will be able to do this in the following way:
db.coll.update({
'_id': 1
},{
$pullAll : {
n.d : {
$lte : new Date(2000, 10, 11)
}
}
})
but the problem is, that it is not working this way.
Any suggestions?
That is not how $pullAll works. You cannot specify a matching condition, you can only specify an array of objects to be deleted (that need to match exactly).
Fortunately, you can use $pull instead (which does accept a matching condition):
db.coll.update({
'_id': 1
},{
$pull : {
n.d : {
$lte : new Date(2000, 10, 11)
}
}
})
Note that $pull also pulls all elements that match, not just one.
This is admittedly a little confusing.

Emit a DateTime in the Map function of MongoDb

My map function looks like this:
map = function()
{
day = Date.UTC(this.TimeStamp.getFullYear(), this.TimeStamp.getMonth(), this.TimeStamp.getDate());
emit({day : day, store_id : this.Store_Id}, {count : 1});
}
TimeStamp is stored as date in the database, like this:
{ "TimeStamp" : "Mon Mar 01 2010 11:58:09 GMT+0000 (BST)", ...}
I need the "day" in the result collection to be stored as a date type as well, but it's stored as long (Epoch ticks) like this:
{ "_id" : { "day" : 1265414400000, "store_id" : 10}, "value" : { "count" : 7 } }
I tried changing the emit to something like this but didn't help:
emit({day : {"$date" : day},...)
Any ideas as to how to do that?
Date.utc is going to return miliseconds from epoch. So when you put your data back into the DB, you can use for example:
new Date(dateAsLong)
and it will be stored as the BSON date format.
earlier than mongo 1.7 it will show up in your hash as:
"Mon Mar 01 2010 11:58:09 GMT+0000 (BST)"
1.7+ it will appear as:
ISODate("2010-03-01T11:58:09Z")