我正在尝试根据用户输入向Cloudant中的文档添加新字段,但是当我执行插入时,我会覆盖文档并丢失现有字段,除非我使用insert
发送它们。
例如,我有一个命令对Cloudant执行查询以接收文档,并返回_id, _rev, name and special_id
字段。此文档将传递给一个新命令,该命令通过插入(例如,我使用favorite_food
)附加字段(例如_rev
)。
现在,用户输入另一个命令,例如"添加school"。这个过程重新开始,我查询Cloudant,然后我的查询返回_id, _rev, name, and special_id
。我在文档中添加了school
字段,执行insert
,然后检查文档,favorite_food
已消失。
是否有一个参数可以传递给Cloudant insert
函数,告诉它实际执行更新而不是覆盖?
FYI这是在Node上运行并使用node-cloudant包。在文档中,我无法找到有关params的更多详细信息,典型的插入内容如下所示:
cloudantDb.insert(doc, params, function(error, response) {
if (!error) {
console.log("success", response);
resolve(response);
} else {
console.log("error", error);
reject(error);
}
});
答案 0 :(得分:1)
没有看到你的代码,很难肯定地说。没有神奇的参数。在Cloudant中,创建,更新和删除之间没有区别 - 它们基本上都是创建修订版本。你不能“修补”JSON文档,你需要每次都提供整个东西。这是一个例子:
# Create a new document
curl -XPOST -H 'content-type:application/json' \
'https://skruger.cloudant.com/testdb' -d '{"name":"stefan"}'
{"ok":true,"id":"5309a1784a9cc45d498e8170af7dcc3c","rev":"1-a0f0b27e1069f45cc121dfe5dc08f280"}
# Add a field
curl -XPUT -H 'content-type:application/json' \
'https://skruger.cloudant.com/testdb/5309a1784a9cc45d498e8170af7dcc3c' \
-d '{"_id":"5309a1784a9cc45d498e8170af7dcc3c", "_rev":"1-a0f0b27e1069f45cc121dfe5dc08f280", "name":"stefan", "fish":"pike pearch"}'
{"ok":true,"id":"5309a1784a9cc45d498e8170af7dcc3c","rev":"2-7c3ea3603c3e16962c7b33f50becc771"}
# Fetch it again
curl 'https://skruger.cloudant.com/testdb/5309a1784a9cc45d498e8170af7dcc3c'
{"_id":"5309a1784a9cc45d498e8170af7dcc3c","_rev":"2-7c3ea3603c3e16962c7b33f50becc771","name":"stefan","fish":"pike pearch"}
# And another new field
curl -XPUT -H 'content-type:application/json' \
'https://skruger.cloudant.com/testdb/5309a1784a9cc45d498e8170af7dcc3c' \
-d '{"_id":"5309a1784a9cc45d498e8170af7dcc3c", "_rev":"2-7c3ea3603c3e16962c7b33f50becc771", "name":"stefan", "fish":"pike pearch", "sport":"tennis"}'
{"ok":true,"id":"5309a1784a9cc45d498e8170af7dcc3c","rev":"3-e0f4d1ab1a47b046ea90a0fbbf34ff36"}
# Fetch again
curl 'https://skruger.cloudant.com/testdb/5309a1784a9cc45d498e8170af7dcc3c'
{"_id":"5309a1784a9cc45d498e8170af7dcc3c","_rev":"3-e0f4d1ab1a47b046ea90a0fbbf34ff36","name":"stefan","fish":"pike pearch","sport":"tennis"}