简体   繁体   English

在Java中正确使用MongoDB更新功能

[英]Right usage of MongoDB update functions in Javascript

I have a probably simple problem using the MongoDB API for updating a document. 使用MongoDB API更新文档时,我可能遇到一个简单的问题。 I have tried several things but nothing was successful. 我尝试了几件事,但没有成功。 Inserting and deleting in contrast work fine. 插入和删除的对比效果很好。

So what am I doing. 那我在做什么 I'm trying to update an existing document with the following approaches: 我正在尝试使用以下方法来更新现有文档:

router.post('/updatecontact', function(req, res) {
    console.log(req.body.id);
    console.log(req.body.data);
    var db = req.db;
    var collection = db.get('contacts');
    collection.update( { '_id': req.body.id }, req.body.data, true );
});

Console output gives me: 控制台输出给我:

5666adefcd80a0f3bdbdebe1
{"_id":"5666adefcd80a0f3bdbdebe1","surname":"Test","firstname":"-","group":"-","organization":[{"type":"Verwaltung","name":"Test-Orga","orgunit":""}],"position":"-"}

so both parameters seem to be okay but the server returns only: 所以两个参数似乎都可以,但是服务器仅返回:

POST /contacts/updatecontact 500 43.911 ms - 1548

The same happens with the replaceOne function replaceOne函数也会发生同样的情况

router.post('/updatecontact', function(req, res) {
    console.log(req.body.id);
    console.log(req.body.data);
    var db = req.db;
    var collection = db.get('contacts');
    collection.replaceOne(
        {'_id': req.body.id},
        JSON.parse(req.body.data), 
        {upsert: true},
        function(err, result){
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});

Whether or not I do a JSON.parse() or use another filter. 是否执行JSON.parse()或使用其他过滤器。

eg {'surname': JSON.parse(req.body.data).surname } the result doesn't change. 例如{'surname': JSON.parse(req.body.data).surname }结果不变。

What's working in contrast is the following insert function where the relevant parts seem to be the same. 相比之下,下面的insert函数的工作原理是相关部分似乎相同。

router.post('/insert', function(req, res) {
    console.log(req.body);
    var db = req.db;
    var collection = db.get('contacts');
    collection.insert(JSON.parse(req.body.data), function(err, result){
        console.log(result);
        console.log('ID is ' + result._id);
        res.send(
            (err === null) ? { oid: result._id } : { oid: '' }
        );
    });
});

Can anyone explain what is wrong? 谁能解释出什么问题了?

I used to doing the update using the save method (req.collection.save). 我曾经使用save方法(req.collection.save)进行更新。 I've a middelware that adds the collection returned by findById to req . 我有一个middelware ,它将findById返回的集合添加到req

var middelware = function(req, res, next){
    yourCollection.findById(req.params.id, function(err, data){
        if(err)
            res.status(500).send(err);
        else if(data){
            req.data= data;
            next();
        }else{
            res.status(404).send('no data found');
        }
    });
}

//this will update parts of your existing resource
var put = function(req, res){
    req.data.a= req.body.a;
    req.data.b= req.body.b;
    ...
    req.data.save(function(err) {
        if(err)
            res.status(500).send(error);
        else
            res.json(req.data);
    }); 
}

var router = express.Router();
router.use('/:id',middelware);
router.route('/')
.put(put);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM