Nodejs mongodb更新数据库的问题

发布于 1周前 作者 h691938207 来自 nodejs/Nestjs

Nodejs mongodb更新数据库的问题

我的mongodb数据库中有这么一个rooms集合 内容如下:

    {
    "_id" : ObjectId("5125df1192601c3017000001"),
    "name": "测试"
    "content" : {
            "posts" : { },
            "comments": [...]
    }

我的roomsDao想通过mongskin操作往 content.posts中增加内容,代码如下:

db.bind("rooms", {
        updateContent: function(id, obj, callback) {
                this.update({"_id": ObjectID.createFromHexString(id)}, {"$set": {"comtent": obj} }, fn );
        }
});
exports.updateContent = function(id, obj, callback) {
         db.rooms.updateContent(id, obj, callback);
}

service中调用时,参数是:

id:   5125df1192601c3017000001
obj: {
    posts: {"user1": {......}},
    comments: []
}

可执行不报错,结果总是没有更新到数据库中,怎么回事呀!!

新手,多多关照!


4 回复

当然可以。根据你提供的信息,问题出在 update 操作的 $set 部分。你将 content 错误地写成了 comtent。此外,你可能希望更新 content.posts 而不是完全替换 content。以下是修正后的代码示例:

修正后的 DAO 层代码

首先,确保你的 roomsDao 文件中的代码正确无误。下面是修正后的代码:

const ObjectID = require('mongodb').ObjectID;

db.bind("rooms", {
    updatePostContent: function(id, postObj, callback) {
        // 使用 $set 操作符来更新 content.posts 的特定部分
        this.update(
            { _id: ObjectID.createFromHexString(id) },
            { $set: { "content.posts": postObj } },
            callback
        );
    }
});

exports.updatePostContent = function(id, postObj, callback) {
    db.rooms.updatePostContent(id, postObj, callback);
};

在 Service 中调用

在服务层中调用 updatePostContent 方法时,确保传递正确的参数:

const roomsDao = require('./path/to/roomsDao');

// 假设 id 和 obj 已经定义
const id = '5125df1192601c3017000001';
const obj = {
    posts: { user1: { /* 具体内容 */ } },
    comments: []
};

roomsDao.updatePostContent(id, obj.posts, (err, result) => {
    if (err) {
        console.error('Error updating content:', err);
    } else {
        console.log('Content updated successfully:', result);
    }
});

解释

  1. 纠正拼写错误:原来的代码中 comtent 应该是 content
  2. 使用 $set 更新特定字段:通过 $set 操作符,你可以指定要更新的具体字段,而不是整个对象。
  3. 传递正确的参数:在调用 updatePostContent 方法时,确保传递的是 postObj 而不是整个 obj

通过以上修改,你应该能够成功更新 MongoDB 中 rooms 集合的 content.posts 字段。如果还有其他问题,请检查是否有其他错误或确认 MongoDB 驱动程序版本是否兼容。


你上面的fn哪来的?

我解决了,是我存的数据的KEY有问题,里面带 了 . 符号

根据你的描述,问题主要出在updateContent方法中的键名错误以及更新逻辑上。在你的MongoDB文档中,content字段包含了postscomments子字段,而在更新操作中你使用了comtent(少了一个’n’),这会导致MongoDB无法正确找到并更新目标字段。

此外,如果你想向content.posts中添加内容,应该使用$set操作符结合点符号来指定路径,或者使用$push操作符来向数组中添加元素。

以下是修正后的示例代码:

const ObjectID = require('mongodb').ObjectID;

db.bind("rooms", {
    updateContent: function(id, obj, callback) {
        // 更新 content.posts 字段
        db.update(
            { _id: ObjectID.createFromHexString(id) }, 
            { $set: { "content.posts": obj.posts } }, 
            callback
        );
    }
});

exports.updateContent = function(id, obj, callback) {
    db.rooms.updateContent(id, obj, callback);
}

在这个例子中,我们假设你想更新或设置content.posts的值为obj.posts。如果你想要添加新的元素到content.posts(假定它是一个对象而非数组),你可能需要根据具体情况调整更新逻辑。

如果content.posts实际上是包含多个键值对的对象,并且你想添加新的键值对而不是完全替换现有对象,可以考虑使用$set操作符配合点符号指定路径,或者使用更复杂的更新策略。

希望这能解决你的问题!如果还有其他疑问,请随时提出。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!