Ho guardato alcune delle domande sul sito e non ho ben capito quello che sto facendo male. Ho un po 'di codice come questo:aggiornamento Mongoose/upsert?
var mongoose = require('mongoose'),
db = mongoose.connect('mongodb://localhost/lastfm'),
Schema = mongoose.Schema,
User = new Schema({
nick: String,
hmask: String,
lastfm: String
});
var UserModel = mongoose.model('User', User);
//Register user to mongodb
var reg_handler = function (act) {
// should add a new entry to the db if nick (act.nick) && hmask (act.host)
// aren't already in the db. Otherwise, update the entry that matches nick
// or hostmask with the new lastfm name (act.params)
};
var get_handler = function (act) {
UserModel.find({ nick: act.params }, function (err, users) {
if (err) { console.log(err) };
users.forEach(function (user) {
console.log('url for user is http://url/' + user.lastfm);
});
});
};
Non sono sicuro di quello che dovrei fare in mezzo c'è per farlo per aggiornare il database in modo corretto. Ho provato parecchie cose, non può annullare per scoprire tutto quello che ho provato però. Mi ci è voluta una gran parte della mia notte e voglio che funzioni.
Questo è quasi quello che voglio, mi chiedo se non v'è alcun modo per fare un OR nella parte condizioni del .update()
var reg_handler = function (act) {
var lfmuser = { nick: act.nick, hmask: act.host, lastfm: act.params };
UserModel.update({ nick: act.nick }, { $set: lfmuser }, { upsert: true }, function(){});
};
terrò giocando intorno con esso.
Si potrebbe voler aggiungere un po 'di gestione degli errori a quest'ultima funzione;) – staackuser2