使用upsert时,我无法将new_data推入数组
如何使用upsert插入/更新一个文档?
history
是一个数组。
顺便说一句,gemmongo
的数据似乎远不如Python
or受欢迎Javascript
。
gemmongo
是否具有比其他语言少的功能并且有更多的bug?
ArgumentError:参数数目错误(1..2为3)
[64] pry(#<Scoot>)> @db.data_collection.find({_id: data[:_id]}).update_one(
[64] pry(#<Scoot>)* {data: data,
[64] pry(#<Scoot>)* from: from_city,
[64] pry(#<Scoot>)* },
[64] pry(#<Scoot>)* {:$push => { "history" => new_data }},
[64] pry(#<Scoot>)* :upsert => true,
[64] pry(#<Scoot>)* :safe => true
[64] pry(#<Scoot>)* )
ArgumentError: wrong number of arguments (3 for 1..2)
Mongo :: Error :: OperationFailure:'$ push'中以美元($)为前缀的字段'$ push'无效,无法存储。(52)
@db.data_collection.find({_id: data[:_id]}).update_one(
{data: data,
from: from_city,
:$push => { "history" => new_data },
},
:upsert => true,
:safe => true
)
collection.find({_id: data[:_id]}).update_one(
{
"$set" => {
"city" => from_city
},
"$push": => { "history" => new_data }
},
:upsert => true
)
SyntaxError: unexpected tSTRING_DEND, expecting end-of-input
[105] pry(#<Scoot>)> :upsert => true
SyntaxError: unexpected =>, expecting end-of-input
:upsert => true
^
[105] pry(#<Scoot>)> )
SyntaxError: unexpected ')', expecting end-of-input
假设您在这里使用2.X驱动程序系列。通常,问题在于:$push
语法不受支持,因此您编写的方式有所不同。此外,:safe
无论您从哪里读到它,都已久不推荐使用。另外,"$set
当您要设置对象的特定字段时,可以使用:
@db.data_collection.find({ _id: data[:_id]}).update_one({
"$set" => {
"data" => data,
"city" => from_city
},
"$push" => { "history" => new_data }
},
:upsert => true
)
请参阅有关的文档 update_one
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句