YouTip LogoYouTip

Mongodb Operators Type

Title: MongoDB $type Operator | Rookie Tutorial\n\nIn MongoDB, the $type operator is used to query documents with fields of a specified type.\n\nThe $type operator in MongoDB is used to query the BSON data type of a field.\n\nIt allows you to specify one or more types and returns documents matching these types.\n\nThe following is a detailed introduction and examples of the $type operator.\n\nSyntax:\n\ndb.collection.find({ field: { $type: } })\n* **field**: The field to check the type of.\n* **type**: The specified BSON type, which can be either the numeric code of the type or the string name of the type.\n\n### BSON Types\n\nThe following are common BSON types along with their corresponding numeric codes and string names:\n\n| Type Code | Type Name |\n| --- | --- |\n| 1 | double |\n| 2 | string |\n| 3 | object |\n| 4 | array |\n| 5 | binData |\n| 6 | undefined |\n| 7 | objectId |\n| 8 | bool |\n| 9 | date |\n| 10 | null |\n| 11 | regex |\n| 12 | dbPointer |\n| 13 | javascript |\n| 14 | symbol |\n| 15 | javascriptWithScope |\n| 16 | int |\n| 17 | timestamp |\n| 18 | long |\n| 19 | decimal |\n| 255 | minKey |\n| 127 | maxKey |\n\n### Examples\n\nFind documents where the field type is string:\n\ndb.myCollection.find({ fieldName: { $type: "string" } })\nOr use the type code:\n\ndb.myCollection.find({ fieldName: { $type: 2 } })\nFind documents where the field type is a number, for example, find documents where the age field type is integer:\n\ndb.myCollection.find({ age: { $type: "int" } })\nOr use the type code:\n\ndb.myCollection.find({ age: { $type: 16 } })\nFind documents where the field type is boolean:\n\ndb.myCollection.find({ isActive: { $type: "bool" } })\nOr use the type code:\n\ndb.myCollection.find({ isActive: { $type:The number 8 } })\nFind documents where the field type is date:\n\ndb.myCollection.find({ createdAt: { $type: "date" } })\nOr use the type code:\n\ndb.myCollection.find({ createdAt: { $type: 9 } })\nFind documents where the field type is one of multiple types, for example, find documents where the value field type is string or integer:\n\ndb.myCollection.find({ value: { $type: ["string", "int"] } })\nOr use the type code:\n\ndb.myCollection.find({ value: { $type: [2, 16] } })\nFind documents where the details field type is object and the score field type is double:\n\n## Example\n\ndb.myCollection.find({\n\n $and:[\n\n{ details:{ $type:"object"}},\n\n{ score:{ $type:"double"}}\n\n]\n\n})\n\nOr use the type code:\n\n## Example\n\ndb.myCollection.find({\n\n $and:[\n\n{ details:{ $type:3}},\n\n{ score:{ $type:1}}\n\n]\n\n})\n\nBy using the $type operator, you can precisely query the data type of specific fields within documents, enabling more granular data filtering and management.\n\n* * *\n\n## More Examples\n\n**The database name we use is "". Our collection name is "col". The following is the data we inserted.**\n\nSimple collection "col":\n\n>db.col.insert({ title: 'PHP Tutorial', description: 'PHP It is a powerful server-side scripting language for creating dynamic interactive sites.', by: '', url: '', tags: ['php'], likes: 200})\n\n>db.col.insert({title: 'Java Tutorial', description: 'Java It is a high-level programming language launched by Sun Microsystems in May 1995.', by: '', url: '', tags: ['java'], likes: 150})\n\n>db.col.insert({title: 'MongoDB Tutorial', description: 'MongoDB It is a NoSQL database.', by: '', url: '', tags: ['mongodb'], likes: 100})\nUse the find() command to view the data:\n\n> db.col.find(){ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP Tutorial", "description" : "PHP It is a powerful server-side scripting language for creating dynamic interactive sites.", "by" : "", "url" : "", "tags" : , "likes" : 200 }{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java Tutorial", "description" : "Java It is a high-level programming language launched by Sun Microsystems in May 1995.", "by" : "", "url" : "", "tags" : , "likes" : 150 }{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB Tutorial", "description" : "MongoDB It is a NoSQL database.", "by" : "", "url" : "", "tags" : , "likes" : 100 }\nIf you want to get data from the "col" collection where the title is of type String, you can use the following command:\n\ndb.col.find({"title" : {$type : 2}})or db.col.find({"title" : {$type : 'string'}})\nThe output result is:\n\n{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP Tutorial", "description" : "PHP It is a powerful server-side scripting language for creating dynamic interactive sites.", "by" : "", "url" : "", "tags" : , "likes" : 200 }{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java Tutorial", "description" : "Java It is a high-level programming language launched by Sun Microsystems in May 1995.", "by" : "", "url" : "", "tags" : , "likes" : 150 }{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB Tutorial", "description" : "MongoDB It is a NoSQL database.", "by" : "", "url" : "", "tags" : , "likes" : 100 }
← Att A CharsetMongodb Operators β†’