也许是时候了,也许是我淹没在稀疏的文档中,无法将自己的头围在Mongoose中的更新概念上:)
这是交易:
我有一个联系模式和模型(缩短的属性):
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var mongooseTypes = require("mongoose-types"),
useTimestamps = mongooseTypes.useTimestamps;
var ContactSchema = new Schema({
phone: {
type: String,
index: {
unique: true,
dropDups: true
}
},
status: {
type: String,
lowercase: true,
trim: true,
default: 'on'
}
});
ContactSchema.plugin(useTimestamps);
var Contact = mongoose.model('Contact', ContactSchema);
我从客户端收到一个包含我需要的字段的请求,并因此使用我的模型:
mongoose.connect(connectionString);
var contact = new Contact({
phone: request.phone,
status: request.status
});
现在我们解决了这个问题:
- 如果我打电话给
contact.save(function(err){...})
我,如果已经存在具有相同电话号码的联系人(如预期-唯一),我将收到一条错误消息 - 我无法致电
update()
联系,因为该方法在文档中不存在 - 如果我对模型调用update:
Contact.update({phone:request.phone}, contact, {upsert: true}, function(err{...})
我会陷入某种无限循环,因为Mongoose更新实现显然不希望将对象作为第二个参数。 - 如果我做同样的事情,但是在第二个参数中,我传递了一个有效的请求属性的关联数组
{status: request.status, phone: request.phone ...}
-但是我没有对特定联系人的引用,也无法找到其createdAt
和updatedAt
属性。
因此,在尝试了所有操作之后,最重要的是:给定了文档contact
,如果文档存在,如何更新,如果文档不存在,如何添加?
谢谢你的时间。
Mongoose现在通过findOneAndUpdate原生支持此功能(调用MongoDB findAndModify)。
如果对象不存在,则upsert = true选项将创建该对象。默认为false。
var query = {'username': req.user.username};
req.newData.username = req.user.username;
MyModel.findOneAndUpdate(query, req.newData, {upsert: true}, function(err, doc) {
if (err) return res.send(500, {error: err});
return res.send('Succesfully saved.');
});
在较旧的版本中,Mongoose不支持使用以下方法的这些钩子:
- 默认值
- 二传手
- 验证者
- 中间件
我只花了3个小时就解决了同样的问题。具体来说,我想“替换”整个文档(如果存在),否则将其插入。解决方法如下:
var contact = new Contact({
phone: request.phone,
status: request.status
});
// Convert the Model instance to a simple object using Model's 'toObject' function
// to prevent weirdness like infinite looping...
var upsertData = contact.toObject();
// Delete the _id property, otherwise Mongo will return a "Mod on _id not allowed" error
delete upsertData._id;
// Do the upsert, which works like this: If no Contact document exists with
// _id = contact.id, then create a new doc using upsertData.
// Otherwise, update the existing doc with upsertData
Contact.update({_id: contact.id}, upsertData, {upsert: true}, function(err{...});
我在Mongoose项目页面上创建了一个问题,要求将有关此信息的信息添加到文档中。
你很亲近
Contact.update({phone:request.phone}, contact, {upsert: true}, function(err){...})
但是您的第二个参数应该是带有修改运算符的对象,例如
Contact.update({phone:request.phone}, {$set: { phone: request.phone }}, {upsert: true}, function(err){...})
好吧,我等待了足够长的时间,没有答案。最后放弃了整个更新/更新方法,并进行了以下操作:
ContactSchema.findOne({phone: request.phone}, function(err, contact) {
if(!err) {
if(!contact) {
contact = new ContactSchema();
contact.phone = request.phone;
}
contact.status = request.status;
contact.save(function(err) {
if(!err) {
console.log("contact " + contact.phone + " created at " + contact.createdAt + " updated at " + contact.updatedAt);
}
else {
console.log("Error: could not save contact " + contact.phone);
}
});
}
});
它行得通吗?是的 我对此感到满意吗?可能不是。2个DB调用而不是一个。
希望将来的Mongoose实现可以提供一个Model.upsert
功能。
您可以通过使用Promises链来实现非常优雅的解决方案:
app.put('url', (req, res) => {
const modelId = req.body.model_id;
const newName = req.body.name;
MyModel.findById(modelId).then((model) => {
return Object.assign(model, {name: newName});
}).then((model) => {
return model.save();
}).then((updatedModel) => {
res.json({
msg: 'model updated',
updatedModel
});
}).catch((err) => {
res.send(err);
});
});
我是猫鼬的维护者。更新文档的更现代方法是使用Model.updateOne()
函数。
await Contact.updateOne({
phone: request.phone
}, { status: request.status }, { upsert: true });
如果您需要升级后的文档,则可以使用 Model.findOneAndUpdate()
const doc = await Contact.findOneAndUpdate({
phone: request.phone
}, { status: request.status }, { upsert: true });
关键要点是,您需要将filter
参数中的唯一属性放入updateOne()
或findOneAndUpdate()
,并将其他属性放入update
参数中。
这是关于的教程 使用Mongoose升级文档。
我创建了一个StackOverflow帐户来回答这个问题。在毫无结果地搜索了网络之后,我自己写了一些东西。这就是我的做法,因此可以将其应用于任何猫鼬模型。导入此函数或将其直接添加到执行更新的代码中。
function upsertObject (src, dest) {
function recursiveFunc (src, dest) {
_.forOwn(src, function (value, key) {
if(_.isObject(value) && _.keys(value).length !== 0) {
dest[key] = dest[key] || {};
recursiveFunc(src[key], dest[key])
} else if (_.isArray(src) && !_.isObject(src[key])) {
dest.set(key, value);
} else {
dest[key] = value;
}
});
}
recursiveFunc(src, dest);
return dest;
}
然后要添加猫鼬文档,请执行以下操作:
YourModel.upsert = function (id, newData, callBack) {
this.findById(id, function (err, oldData) {
if(err) {
callBack(err);
} else {
upsertObject(newData, oldData).save(callBack);
}
});
};
此解决方案可能需要进行2次DB调用,但是您确实可以从中受益,
- 针对模型的架构验证,因为您正在使用.save()
- 您可以在更新调用中向上插入深层嵌套的对象而无需手动枚举,因此,如果模型发生更改,则不必担心更新代码
请记住,即使源具有现有值,目标对象也将始终覆盖源。
同样,对于数组,如果现有对象的数组长于替换对象的数组,则旧数组末尾的值将保留。向上插入整个数组的一种简单方法是,如果您打算这样做,则在向上插入之前将旧数组设置为空数组。
更新-01/16/2016我添加了一个额外的条件,如果存在原始值数组,Mongoose不会意识到该数组在不使用“ set”函数的情况下就已更新。
我需要将文档更新/更新到一个集合中,我要做的是创建一个像这样的新对象文字:
notificationObject = {
user_id: user.user_id,
feed: {
feed_id: feed.feed_id,
channel_id: feed.channel_id,
feed_title: ''
}
};
由我从数据库其他地方获得的数据组成,然后在模型上调用update
Notification.update(notificationObject, notificationObject, {upsert: true}, function(err, num, n){
if(err){
throw err;
}
console.log(num, n);
});
这是我第一次运行脚本后得到的输出:
1 { updatedExisting: false,
upserted: 5289267a861b659b6a00c638,
n: 1,
connectionId: 11,
err: null,
ok: 1 }
这是我第二次运行脚本时的输出:
1 { updatedExisting: true, n: 1, connectionId: 18, err: null, ok: 1 }
我正在使用猫鼬版本3.6.16
app.put('url', function(req, res) {
// use our bear model to find the bear we want
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
bear.name = req.body.name; // update the bears info
// save the bear
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear updated!' });
});
});
});
这是解决猫鼬更新方法的更好方法,您可以查看Scotch.io了解更多详细信息。这绝对对我有用!!!
2.6中引入了一个错误,该错误也影响到2.7
该upsert在2.4上正常工作
https://groups.google.com/forum/#!topic/mongodb-user/UcKvx4p4hnY
https://jira.mongodb.org/browse/SERVER-13843
看一下,其中包含一些重要信息
更新:
这并不意味着upsert不起作用。这是一个很好的用法示例:
User.findByIdAndUpdate(userId, {online: true, $setOnInsert: {username: username, friends: []}}, {upsert: true})
.populate('friends')
.exec(function (err, user) {
if (err) throw err;
console.log(user);
// Emit load event
socket.emit('load', user);
});
您可以以此简单地更新记录并获得更新的数据以作为响应
router.patch('/:id', (req, res, next) => {
const id = req.params.id;
Product.findByIdAndUpdate(id, req.body, {
new: true
},
function(err, model) {
if (!err) {
res.status(201).json({
data: model
});
} else {
res.status(500).json({
message: "not found any relative data"
})
}
});
});
这对我有用。
app.put('/student/:id', (req, res) => {
Student.findByIdAndUpdate(req.params.id, req.body, (err, user) => {
if (err) {
return res
.status(500)
.send({error: "unsuccessful"})
};
res.send({success: "success"});
});
});
这是创建/更新同时调用中间件和验证器的最简单方法。
Contact.findOne({ phone: request.phone }, (err, doc) => {
const contact = (doc) ? doc.set(request) : new Contact(request);
contact.save((saveErr, savedContact) => {
if (saveErr) throw saveErr;
console.log(savedContact);
});
})
对于到达这里仍在寻找具有钩子支持的“插入”好的解决方案的任何人,这是我已经测试和工作的。它仍然需要2个DB调用,但是比我在单个调用中尝试过的任何操作都要稳定得多。
// Create or update a Person by unique email.
// @param person - a new or existing Person
function savePerson(person, done) {
var fieldsToUpdate = ['name', 'phone', 'address'];
Person.findOne({
email: person.email
}, function(err, toUpdate) {
if (err) {
done(err);
}
if (toUpdate) {
// Mongoose object have extra properties, we can either omit those props
// or specify which ones we want to update. I chose to update the ones I know exist
// to avoid breaking things if Mongoose objects change in the future.
_.merge(toUpdate, _.pick(person, fieldsToUpdate));
} else {
toUpdate = person;
}
toUpdate.save(function(err, updated, numberAffected) {
if (err) {
done(err);
}
done(null, updated, numberAffected);
});
});
}
如果有发电机,它将变得更加容易:
var query = {'username':this.req.user.username};
this.req.newData.username = this.req.user.username;
this.body = yield MyModel.findOneAndUpdate(query, this.req.newData).exec();
没有其他解决方案对我有用。我正在使用发布请求并更新数据(如果找到的话),否则将其插入,还发送_id并与需要删除的请求正文一起发送。
router.post('/user/createOrUpdate', function(req,res){
var request_data = req.body;
var userModel = new User(request_data);
var upsertData = userModel.toObject();
delete upsertData._id;
var currentUserId;
if (request_data._id || request_data._id !== '') {
currentUserId = new mongoose.mongo.ObjectId(request_data._id);
} else {
currentUserId = new mongoose.mongo.ObjectId();
}
User.update({_id: currentUserId}, upsertData, {upsert: true},
function (err) {
if (err) throw err;
}
);
res.redirect('/home');
});
//Here is my code to it... work like ninj
router.param('contractor', function(req, res, next, id) {
var query = Contractors.findById(id);
query.exec(function (err, contractor){
if (err) { return next(err); }
if (!contractor) { return next(new Error("can't find contractor")); }
req.contractor = contractor;
return next();
});
});
router.get('/contractors/:contractor/save', function(req, res, next) {
contractor = req.contractor ;
contractor.update({'_id':contractor._id},{upsert: true},function(err,contractor){
if(err){
res.json(err);
return next();
}
return res.json(contractor);
});
});
--
User.findByIdAndUpdate(req.param('userId'), req.body, (err, user) => {
if(err) return res.json(err);
res.json({ success: true });
});
按照Traveling Tech Guy的回答(已经很棒),我们可以创建一个插件并将其初始化后附加到mongoose上,以便 .upsert()
所有型号都可用。
plugins.js
export default (schema, options) => {
schema.statics.upsert = async function(query, data) {
let record = await this.findOne(query)
if (!record) {
record = new this(data)
} else {
Object.keys(data).forEach(k => {
record[k] = data[k]
})
}
return await record.save()
}
}
db.js
import mongoose from 'mongoose'
import Plugins from './plugins'
mongoose.connect({ ... })
mongoose.plugin(Plugins)
export default mongoose
然后,你可以这样做User.upsert({ _id: 1 }, { foo: 'bar' })
或YouModel.upsert({ bar: 'foo' }, { value: 1 })
只要你想。
过了一会儿,我才回到这个问题,并决定根据Aaron Mast的答案发布一个插件。
https://www.npmjs.com/package/mongoose-recursive-upsert
用作猫鼬插件。它设置了一个静态方法,该方法将递归合并传入的对象。
Model.upsert({unique: 'value'}, updateObject});
这个Coffeescript对我来说可以在Node上工作-诀窍是_id get在从客户端发送和返回时被剥夺了ObjectID包装器,因此需要替换以进行更新(当未提供_id时,保存将还原为插入并添加一)。
app.post '/new', (req, res) ->
# post data becomes .query
data = req.query
coll = db.collection 'restos'
data._id = ObjectID(data._id) if data._id
coll.save data, {safe:true}, (err, result) ->
console.log("error: "+err) if err
return res.send 500, err if err
console.log(result)
return res.send 200, JSON.stringify result
以马丁·库兹杜维奇(Martin Kuzdowicz)上面发布的内容为基础。我使用以下内容使用猫鼬和json对象的深层合并进行更新。与mongoose中的model.save()函数一起,它允许mongoose进行完全验证,即使是依赖于json中其他值的验证。它确实需要deepmerge软件包https://www.npmjs.com/package/deepmerge。但这是一个非常轻巧的包装。
var merge = require('deepmerge');
app.put('url', (req, res) => {
const modelId = req.body.model_id;
MyModel.findById(modelId).then((model) => {
return Object.assign(model, merge(model.toObject(), req.body));
}).then((model) => {
return model.save();
}).then((updatedModel) => {
res.json({
msg: 'model updated',
updatedModel
});
}).catch((err) => {
res.send(err);
});
});
阅读以上内容后,我决定使用此代码:
itemModel.findOne({'pid':obj.pid},function(e,r){
if(r!=null)
{
itemModel.update({'pid':obj.pid},obj,{upsert:true},cb);
}
else
{
var item=new itemModel(obj);
item.save(cb);
}
});
如果r为null,则创建新项目。否则,请在更新中使用upsert,因为更新不会创建新项目。
文章标签:javascript , mongodb , mongoose , node.js
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!