7 Replies Latest reply on Feb 2, 2018 2:57 PM by rareddy

    How do I write ObjectId type field data to mongodb?

    kevinyjn

      I am using Teiid Designer to save data from oracle to mongodb, however there are fields with ObjectId type that references another ObjectId type field of table, I have tried to use update A.objfield = B.objfield, however, A.objfield becomes string type that stores hex string, how can I save the ObjectId type data to A.objfield?

        • 1. Re: How do I write ObjectId type field data to mongodb?
          rareddy

          The question is vague. You have not given any context of what you are doing? Give me an example and how you would like to see in MongoDB.

           

          In Teiid with MongoDB as source, the Primary Key of the table maps to the "_id" field in MongoDB. If you do not have a Primary Key then MongoDB still creates one implicitly in document model. With MongoDB, to manage relations from Table A to Table B, they need to be designed as such and define related extension properties carefully. Read and understand from here [1]. If you already have a document model in MongoDB then try using metadata import feature to design to Table structure automatically.

           

          [1] MongoDB Translator · Teiid Documentation

          • 2. Re: How do I write ObjectId type field data to mongodb?
            kevinyjn

            Ramesh,

            Thank you for replying my question, the example is like bellow, the field parentid were referenced by _id, the front 4 rows were generated manually, these types of parentid were what I expected, but the bellow rows were generated by teiid, it becomes to string type.

            The table defineation is :

            Part of virtual procedure clause is:

             

            LOOP ON (SELECT CommonSrcOrganization.GY_KSDM.KSDM, CommonSrcOrganization.GY_KSDM.SJKS FROM CommonSrcOrganization.GY_KSDM) AS ksrow

              BEGIN

              UPDATE GuanZhong.Organization SET parentId = (SELECT GuanZhong.Organization.id FROM GuanZhong.Organization WHERE GuanZhong.Organization.code = convert(ksrow.SJKS, string)) WHERE GuanZhong.Organization.code = convert(ksrow.KSDM, string);

              END

            • 3. Re: How do I write ObjectId type field data to mongodb?
              rareddy

              If the document is already in the MongoDB, then use

               

              File -> Import -> Teiid Designer -> Teiid Connection >> Source Model

               

              and point to the MongoDB database, and use translator mongodb and import metadata. That will generate correct metadata to begin with.

              • 4. Re: How do I write ObjectId type field data to mongodb?
                kevinyjn

                Ramesh,

                Thank you for pointing me the operation step, I've followed the step to import MongoDB metadata, but it effects the same as I manually created metadata, the field parentId were assigned with field "_id", but it was saved into hex string type instead of ObjectId type. I want to save it into ObjectId type so that out client developer can correctly access the data.

                • 5. Re: How do I write ObjectId type field data to mongodb?
                  rareddy

                  How did you create a connection to MongoDB in the Teiid Server.  When using the above procedure, the first step is to select or create a connection. There you need to create a MongoDB connection and use that. If there already connection based some other JDBC drivers Teiid does not support that. So, try creating a new connection and using that from Designer. Otherwise look into Teiid documentation here [1] on how to create MongoDB connection on the server without Designer and select the JNDI name during the import.

                   

                  [1] MongoDB Data Sources · Teiid Documentation

                  • 6. Re: How do I write ObjectId type field data to mongodb?
                    kevinyjn

                    Hi, Ramesh, I created the connection to MongoDB using mongodb translator, and I followed your step to import the source model, the ObjectId fields were imported as object type. but when I save the ObjectId field references from other ObjectId field using UPDATE clause, the updated ObjectId field value becomes hex string formation, I guess the ObjectId that referenced were convert into hex string by mongodb translator, I don't know what formation of ObjectId field were stored to mongo db, I'm thinking of a solution to convert the hex string type ObjectId field data into the right formation that would be saved to mongo db, will you please give me some suggestion to solve this problem?

                     

                    Kevin

                    • 7. Re: How do I write ObjectId type field data to mongodb?
                      rareddy

                      According to my code I set the ObjectId as String [1], I am not sure how it is being converted to Object. You can try converting all the "_id" fields as String, and then set native type as "org.bson.types.ObjectId" and auto increment to true. If that does not work I would have debug and see why that is the case.

                       

                      [1] teiid/MongoDBMetadataProcessor.java at master · rareddy/teiid · GitHub