I want to save multiple documents which are tags found on a post indicated by hash sign '#'. I have tags in an array. for example:
var tags = ['banana', 'apple', 'orange', 'pie']
I loop over them and convert the to document objects (to insert them into DB). the problem is that I want to insert a new document to collection only if it never inserted before. and if it inserted before I want to increment inserted document's userCounter
property by one.
var tags = ['banana', 'apple', 'pie', 'orange'];
var docs = tags.map(function(tagTitle) {
return {
title: tagTitle,
// useCounter: ??????????
};
});
var Hashtag = require('./models/Hashtag');
/**
* it creates multiple documents even if a document already exists in the collection,
* I want to increment useCounter property of each document if they exist in the collection;
* not creating new ones.
* for example if a document with title ptoperty of 'banana' is inserted before, now increment
* document useCounter value by one. and if apple never inserted to collection, create a new document
* with title of 'apple' and set '1' as its initial useCounter value
*/
Hashtag.create(docs)
.then(function(createdDocs) {
})
.then(null, function(err) {
//handle errors
});