node.js - What's the best way to handle insert unique, okay to overwrite? -
i want maintain collection of geographical info ip addresses.
var ipaddressschema = new schema({ ip: { type: string, unique: true }, geo: object }, { strict: true });
ips should unique. , if ip exists, should overwrite document.
function storegeotomongo() { emitter.on('inredis', function (geodata, ipstring) { console.log('.... store geoinfo mongodb ' + ipstring); ipaddress.findoneandupdate({ ip: ipstring }, { ip: ipstring, geo: geodata }, { upsert: true }, function (err, document) { if (err) { console.log(err); } else { console.log('successfully saved document'); } }); } ); }
is there above should improved? (thanks, neil.)
Comments
Post a Comment