node js , mongodb : check if an object already exist

Viewed 1050

I'am newbie to nodejs and mongodb, so how can I check if an object already exist in the collections , Note that my field type in the schema is object or JSON

const BillSchema = mongoose.Schema(
    {

        content: {
            type: Object //or JSON
        },
    }
);

const Bill = module.exports = mongoose.model('Bill', BillSchema);

module.exports.addBill = function (newBill, callback) {
    //Check for all bill titles and content, if newBill doesn't exist then add else do nothing
    Bill.count({ content: newBill.content }, function (err, count) {
        //count == 0 always ???
        if (err) {
            return callback(err, null);

        } else {
            if (count > 0) {
                //The bill already exists in db
                console.log('Bill already added');
                return callback(null, null);
            } else {   //The bill doesnt appear in the db
                newBill.save(callback);
                console.log('Bill added');
            }
        }
    });
}
1 Answers
Related