[英]How to insert JSON object with “$” field into Meteor collection?
I am retrieving data about books from the Amazon API and storing it as a JSON object. 我正在从Amazon API检索有关书籍的数据,并将其存储为JSON对象。 I then want to insert this data into a Meteor collection. 然后,我想将此数据插入到Meteor集合中。 However, some fields in the JSON data begin with the "$" character, which leads to this error: 但是,JSON数据中的某些字段以“ $”字符开头,从而导致此错误:
MongoError: The dollar ($) prefixed field '$' in 'result.results.0.ItemAttributes.0.ManufacturerMaximumAge.0.$' is not valid for storage. MongoError:“ result.results.0.ItemAttributes.0.ManufacturerMaximumAge.0。$”中以美元($)为前缀的字段“ $”对存储无效。
I would like help to insert data into the collection given that the data contains "$" signs. 鉴于数据包含“ $”符号,我希望将数据插入集合中。 If that is not possible,is there a way to remove all occurrences of "$" in the JSON data and THEN store it into the Meteor collection? 如果不可能,是否有办法删除JSON数据中所有出现的“ $”,然后将其存储到Meteor集合中? Here is my code, given a Book title and author, that searches the Amazon API and returns information about the first 10 results: (SearchResult is the reference to the collection) 这是我的代码,具有书名和作者名,该代码搜索Amazon API并返回有关前10个结果的信息:(SearchResult是对该集合的引用)
Meteor.methods({
itemSearch(appUUID,title, author) {
var result = {};
result.search = {
appUUID: appUUID,
title: title,
author: author
}
client.itemSearch({
title: title,
author: author,
searchIndex: 'Books',
responseGroup: 'ItemAttributes, Images'
}).then(function(results){
result.results = results;
var upsertResult = SearchResult.upsert( {
appUUID: appUUID,
title: title,
author: author,
},
{
$set: {result: result}
}
);
}).catch(function(err){
SearchResult.upsert({
appUUID: appUUID,
title: title,
author: author,
},
{
$set: {result: result }
}
);
});
}
});
This is the JSON data, and the dollar sign before "Units" is what is causing the issue. 这是JSON数据,导致此问题的原因是“单位”前的美元符号。
"Width": [
{
"_": "810",
"$": {
"Units": "hundredths-inches"
}
}
]
Simple answer is, no, you cannot use the $
in fields in MongoDB (from the docs ): 简单的答案是,不,您不能使用MongoDB中的$
in字段(来自docs ):
The field names cannot start with the dollar sign ($) character. 字段名称不能以美元符号($)开头。
There are many possible solutions to change your data into a format that MongoDB can accept, but to give you the simplest solution I can think of, you can just map the keys of the object. 有很多可能的解决方案将数据更改为MongoDB可以接受的格式,但是要给您我能想到的最简单的解决方案,您只需映射对象的键即可。 For example, with lodash's mapKeys
function : 例如,使用lodash的mapKeys
函数 :
data = _.mapKeys(data, (value, key) => {
if(key === '$') {
return 'some_custom_key'
} else {
return key
}
})
This would change all your $
into some_custom_key
. 这会将您的所有$
更改为some_custom_key
。 As long as you have hooks (such as Mongoose's pre save
middleware and post read
middleware, docs ) that can convert this for you under-the-hood, it should be a somewhat workable solution. 只要您具有挂钩(例如Mongoose的save
前中间件和read
中间件docs )等可以在后台为您转换的钩子,它就应该是一个可行的解决方案。
I don't know if it's possible, but a better solution would be to get Amazon API to give you the data without $
, but I can't speculate about that since I'm unfamiliar with that API. 我不知道是否可能,但是更好的解决方案是让Amazon API在没有$
的情况下$
您提供数据,但是由于我不熟悉该API,所以我无法对此进行推测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.