Mongo check if a document already exists Mongo check if a document already exists mongoose mongoose

Mongo check if a document already exists


Create a compound index and make it unique.Using the index mentioned above will ensure that there are no documents which have the same for_user and stream.

trackSchema.ensureIndex( {for_user:1, stream:1}, {unique, true} )

Now use the mongoDB batch operation to insert multiple documents.

//docs is the array of tracks you are going to insert.trackTable.collection.insert(docs, options, function(err,savedDocs){ //savedDocs is the array of docs saved. //By checking savedDocs you can see how many tracks were actually inserted})

Make sure to validate your objects as by using .collection we are bypassing mongoose.


Make a unique _id based on user and track. In mongo you can pass in the _id that you want to use.

Example {_id : "NicoleMoudaber InTheMOODEpisode14", artist: "Nicole Moudaber" artwork: "https://i1.sndcdn.com/artworks-000087731284-gevxfm-large.jpg?e76cf77" source: "soundcloud" stream: "https://api.soundcloud.com/tracks/162626499/stream.mp3? client_id=7d7e31b7e9ae5dc73586fcd143574550" title: "In The MOOD - Episode 14"}

_id must be unique and won't let you insert another document with the same _id. You could also use this to find the record later db.collection.find({_id : NicoleMoudaber InTheMOODEpisode14})

or you could find all tracks for db.collection.find({_id : /^NicoleMoudaber/}) and it will still use the index.

There is another method to this that I can explain if you dont' like this one.

Both options will work in a sharded environment as well as a single replica set. "Unique" indexes do not work in a sharded environment.


Soundcloud API provides a track id, just use it.then before inserting datas you make a

tracks.find({id_soundcloud : 25645456}).exec(function(err,track){     if(track.length){ console.log("do nothing")}else {//insert} });