Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Query BinData by Type in MongoDB
To query BinData documents by their subtype in MongoDB, use the subtype() method within a JavaScript function to filter documents based on their binary data type.
Syntax
db.collection.find(function(){
return this.fieldName.subtype() == subtypeNumber
});
Sample Data
Let us create a collection with documents containing different BinData subtypes ?
db.demo249.insertMany([
{ "_id": BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") },
{ "_id": BinData(4, "CNDF66qIlCY92q1vFAAAAQ==") },
{ "_id": BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }
]);
{
"acknowledged": true,
"insertedIds": {
"0": BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv"),
"1": UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001"),
"2": BinData(3, "CNDF66qJ29g92q1vFAAAEw==")
}
}
Display all documents from the collection ?
db.demo249.find();
{ "_id": BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }
{ "_id": UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") }
{ "_id": BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }
Example: Query BinData by Subtype 0
To find documents with BinData subtype 0 (Generic binary subtype) ?
db.demo249.find(function(){
return this._id.subtype() == 0
});
{ "_id": BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }
Key Points
- BinData subtypes: 0 (Generic), 3 (UUID old), 4 (UUID standard), 5 (MD5), etc.
- The
subtype()method returns the numeric subtype of the BinData field. - Use JavaScript functions with
find()to filter by subtype values.
Conclusion
Use the subtype() method within JavaScript functions to query MongoDB documents by their BinData subtype. This approach allows filtering binary data based on specific type classifications.
Advertisements
