文档字段名称不能以' $'开头。 (坏键:'圣诞节混合咖啡减$ 3 - 1磅+地面')

时间:2018-04-04 09:15:57

标签: java mongodb spring-mongodb

我的对象列表如下所示。

{
            "altText": "",
            "anchorText": "$3 Off Christmas Blend Coffee - 1 Lb. Ground",
            "anchorType": "href",
            "domainRating": 83,
            "externalLinks": 41,
            "internalLinks": 100,
            "lastVisited": 0,
            "lastVisitedDateString": "June 23, 2017",
            "noFollow": false,
            "noFollowUrl": "Do Follow",
            "rating": 13,
            "sourceURL": "https://www.goodsearch.com/coffee-category/coupons",
            "targetURL": "https://www.priesters.com/",
            "title": "Coffee Coupons, Promo Codes, Deals, Discounts - Goodshop"
        },
        {
            "altText": "",
            "anchorText": "$3 Off Christmas Blend Coffee - 1 Lb. Ground",
            "anchorType": "href",
            "domainRating": 83,
            "externalLinks": 41,
            "internalLinks": 100,
            "lastVisited": 0,
            "lastVisitedDateString": "June 23, 2017",
            "noFollow": false,
            "noFollowUrl": "Do Follow",
            "rating": 13,
            "sourceURL": "https://www.goodsearch.com/coffee-category/coupons",
            "targetURL": "http://www.priesters.com/",
            "title": "Coffee Coupons, Promo Codes, Deals, Discounts - Goodshop"
        }

在上面的对象列表中,有一个名为" anchorText"的字段。此字段值以" $"开头。但它没有在Mongo系列中获得保存。

Mongodb中的Java代码保存集合:

if (!mongoTemplate.collectionExists(COLLECTION)) {
            mongoTemplate.createCollection(COLLECTION);
        }
        mongoTemplate.save(webStats, COLLECTION);

如何解决此问题。

1 个答案:

答案 0 :(得分:0)

在将文档插入集合时,使用$值不应该是一个问题。在你的情况下,$用在值而不是键中,如果在键中使用了$,那么在插入文档时你会在mongo中出错。

 "key" : "$value" // is fine

 "anchorText": "$3 Off Christmas Blend Coffee - 1 Lb. Ground" // allowed in mongodb

 "$key" : "value" // Error: field names cannot start with $

保存文档时遇到的错误不是因为$ in值,而是可能还有其他原因。