Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

There is a period (.) in the attributes of the mongo data collection. The solution of

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Preface

MongoDB is a document-based database for collective storage, and the basic concepts involved are different from those of relational databases. This paper mainly introduces the existence period (.) of the attribute of mongo data set. Let's take a look at the detailed introduction.

Basic knowledge points:

1. It seems that mongo3.6 did not allow the insertion of keys with dots (.) or dollar signs ($) before, but when I used the mongoimport tool to import a JSON file containing dots, it worked fine.

two。 When using spring-data-mongodb to process the addition, deletion, modification and query of mongodb, the data will be converted through a MappingMongoConverter (Document and Modle conversion class).

3. The specific conversion of dots is DBObjectAccessor (spring-data-mongodb-1.10.13) or DocumentAccessor (spring-data-mongodb-2.0.9), as follows:

/ / convert public void put (MongoPersistentProperty prop, Object value) {Assert.notNull (prop, "MongoPersistentProperty must not be null!"); String fieldName = prop.getFieldName (); if (! fieldName.contains (".")) {dbObject.put (fieldName, value); return;} Iterator parts = Arrays.asList (fieldName.split ("\\."). Iterator (); DBObject dbObject = this.dbObject; while (parts.hasNext ()) {String part = parts.next () If (parts.hasNext ()) {dbObject = getOrCreateNestedDbObject (part, dbObject);} else {dbObject.put (part, value);} / / transform public Object get (MongoPersistentProperty property) {String fieldName = property.getFieldName (); if (! fieldName.contains (".)) {return this.dbObject.get (fieldName);} Iterator parts = Arrays.asList (fieldName.split ("\\. ")). Iterator (); Map source = this.dbObject; Object result = null While (source! = null & & parts.hasNext ()) {result = source.get (parts.next ()); if (parts.hasNext ()) {source = getAsMap (result);}} return result;} / / determine whether the value is empty public boolean hasValue (MongoPersistentProperty property) {Assert.notNull (property, "Property must not be null!"); String fieldName = property.getFieldName (); if (! fieldName.contains (".")) {return this.dbObject.containsField (fieldName) } String [] parts = fieldName.split ("\\."); Map source = this.dbObject; Object result = null; for (int I = 1; I < parts.length; itemized +) {result = source.get (parts [I-1]); source = getAsMap (result); if (source = null) {return false;} return source.containsKey (parts [parts.length-1]);}

4. The meaning that a dot has a subset in mongodb

For example, query A.B attribute: query the value of attribute B in the subset corresponding to An in the collection, not the property of A.B in the query collection.

Problem description: what the document looks like in the database:

{"_ id": ObjectId ("5bae00765500af6307755111"), "name": "java", "age": 26, "A.B": "nnnn"}

Therefore, you cannot query the value of "A.B" in the collection using @ Field ("A.B") in Model.

@ Field ("A.B") @ JSONField (serialzeFeatures = SerializerFeature.DisableCircularReferenceDetect) private Integer ab;

5. Solution:

Access to multi-party materials have the following experience: dots can be inserted in MongoDB should start at version 3.6, although official documents can support dots, but the third-party driver, spring-data-mongodb does not support, but because the project has used spring-data-mongodb is difficult to replace, so think of overlay conversion method.

How do I overwrite the files in the spring-data-mongodb package?

Create a new directory like the DBObjectAccessor conversion file, rebuild the DBObjectAccessor class to copy the code custom changes, compile or give priority to the new class.

/ / convert public Object get (MongoPersistentProperty property) {String fieldName = property.getFieldName (); return this.dbObject.get (fieldName);} / / determine whether the value is empty public boolean hasValue (MongoPersistentProperty property) {Assert.notNull (property, "Property must not be null!"); String fieldName = property.getFieldName (); return this.dbObject.containsField (fieldName);}

Note: do not modify the put method as much as possible. The period is not supported in the lower version of MongoDB, and an error will be reported if inserted.

Of course, it is best not to have a dot in the attribute.

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report