0

My Object list is like below.

{
            "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"
        }

In the above object list, there is one field called "anchorText". This field value started with "$" but It's not getting saving in Mongo collection.

Java code saving collection in Mongodb:

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

How can I fix this issue.

vemanna
  • 41
  • 1
  • 3

1 Answers1

0

Having $ in value shouldn't be a problem while inserting a document into a collection. In your case $ is used in the value and not in the key, if $ is used in the key then you will get an error in mongo while inserting the document.

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

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

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

Error you are facing while saving your document is not because of $ in value, but there might be other reasons.

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
  • Thank you Clement Amarnath for your quick reply. Even I am wondering why I am getting this error but you can see the key is "anchorText" and the value starts with "$". – vemanna Apr 04 '18 at 09:49
  • Try executing the insert query directly in the mongo shell and see what happens, if it is successful then the problem is narrowed and it should be with your mongodb java driver. – Clement Amarnath Apr 04 '18 at 10:01
  • Through Mongo shell inserting successfully. But from java I am getting error. I am using spring-data-mongodb 1.9.5 RELEASE org.springframework.data spring-data-mongodb 1.9.5.RELEASE – vemanna Apr 04 '18 at 10:43