3

How do I explicitly define data types for a MongoDB collection?

I understand that in Mongo if a collection doesn't exist it will create it when you insert the document, but I know the fields and data types of the collection before hand and I want to create it. I have created an example collection using:

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )

According to this document from the MongoDB documentation, there are various data types in MongoDB, but again, How do I explicitly define data types for a MongoDB collection?

  • Why you want to do that? you need to produce an error if a string inserted on numeric field? Generally this is against mongo general purposes. – Antonios Aug 5 '14 at 12:54
2

I had found this a while ago for the possibility of validating json data against a schema. If you wanted to do something like this, however, there's stuff you can do in javascript inside the database.

  • This is a better answer. – ConfusedDeer Aug 6 '14 at 21:42
2

MongoDB is schemaless so it makes no sense to talk about a collection having fields and data types. You have to handle it in your own code if you want documents in a collection to have specific fields - MongoDB can't help you there.

  • There are strings data types and there are number data types, just to name two. What are these used for? – ConfusedDeer Aug 5 '14 at 14:18
  • They are used when storing data. Which type to use when is usually decided by your driver depending on your input. – Martin Aug 5 '14 at 20:40
  • In your original question you ask "How do I explicitly define data types for a MongoDB collection?" What do you exactly mean by that - what is it you want to accomplish? – Martin Aug 5 '14 at 20:42
  • 1
    I have since read how to explicitly define in JSON a string or a number a string would be like {"myNumbers":{"myString":"5"}}, but a number would be stored as {"myNumbers":{"myNumber": 5}}. I'm clear now. I thought I had to define the datatype. I see everything is stored as a number or string and utilized by my driver as I need. – ConfusedDeer Aug 5 '14 at 20:58
  • @ConfusedDeer - It's not correct to say that everything in mongodb is stored as number or string. Data in mongodb is stored in BSON format, which defines a large number of specific data types. You can see the currently supported types here – UpTheCreek Jan 5 '16 at 12:54
2

Use the mongoDB document validator. It was introduced in version 3.2 and allow you to validate documents during updates and insertions. It need to be defined when the collection is being created.

You can define the data types for a collection like this:

db.createCollection( "log",
   { validator: { $and:
      [
         { capped: { $type: "bool" } },
         { size: { $type: "int" } },
         { max: { $type: "int" } }
      ]
   }
} )

Document validator support types:

https://docs.mongodb.com/manual/reference/operator/query/type/

Official info about document validator:

https://docs.mongodb.com/manual/core/document-validation/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.