javascript - MongoDB reject duplicates with compound unique index -


i'm trying reject duplicates when save new documents.

my documents this:

{ "station_id" : "klfi",   "timeasdate" : 1437656400000,    "time" : "231500z",    "myvisibility" : 9600,    "skyconditions" : { "1" : { "cloud_base_ft_amsl" : 5009.8424,                               "cloud_base_ft_agl" : "5000",                                "sky_cover" : 3 },                       "2" : 0,                        "3" : 0                       },   "latitude" : 37.07,    "longitude" : -76.37,    "_id" : objectid("55b0e0df80e44b30365e41de"),    "__v" : 0  } 

while importing new documents be, there duplicates want reject documents, have same station_id , time (only in same document). if station_id same , time different want keep it.

i believe can compound unique index this:

db.meteo.createindex({time: 1, station_id: 1}, {unique: true, dropdups: true}) 

but doesn't realy works how want, rejects entries, instead of having more or less 60'000 entries have 7000.

i'm not javascript or mongodb expert , i'm not shure on right track, can me please?

regards

if understand correctly think can use upsert option on update

this allows enter update query, such { station_id: "klfi" } , pass in document update.

using upsert : true flag mean documents update query not met created new documents, duplicates not created.

this assumes if station_id , timeasdate not changed, rest of document not changed.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -