
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB query to find records with keys containing dots?
For this, use $addFields. In that, use the $objectToArray to get the data in the form of keys and values. Use $filter with $indexOfBytes to check if there are any keys with. inside it.
Let us first create a collection with documents −
> db.demo364.insertOne( ... { ... "details" : { ... "details1.otherdetails.Name" : {"FirstName":"Chris" } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e57d485d0ada61456dc936d") } > db.demo364.insertOne( ... { ... "details" : { ... "details1.otherdetails.Name" : {"FirstName":"David" } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e57d486d0ada61456dc936e") }
Display all documents from a collection with the help of find() method −
> db.demo364.find();
This will produce the following output −
{ "_id" : ObjectId("5e57d485d0ada61456dc936d"), "details" : { "details1.otherdetails.Name" : { "FirstName" : "Chris" } } } { "_id" : ObjectId("5e57d486d0ada61456dc936e"), "details" : { "details1.otherdetails.Name" : { "FirstName" : "David" } } }
Following is the query to find records with keys containing dots −
> db.demo364.aggregate([ ... { ... $addFields: { ... result: { ... $filter: { ... input: { $objectToArray: "$details" }, ... cond: { ... $ne: [ { $indexOfBytes: [ "$$this.k", "." ] } , -1 ] ... } ... } ... } ... } ... }, ... { ... $match: { ... $expr: { ... $ne: [ { $size: "$result" }, 0 ] ... } ... } ... }, ... { ... $project: { ... result: 0 ... } ... } ... ])
This will produce the following output −
{ "_id" : ObjectId("5e57d485d0ada61456dc936d"), "details" : { "details1.otherdetails.Name" : { "FirstName" : "Chris" } } } { "_id" : ObjectId("5e57d486d0ada61456dc936e"), "details" : { "details1.otherdetails.Name" : { "FirstName" : "David" } } }
- Related Articles
- MySQL LIKE command doesn't work with strings containing dots to display records beginning with a specific number
- MongoDB query to find oldest date of three keys in each document
- Return all ages records that are ints with a MongoDB query
- MongoDB query to retrieve records from a collection named with letters and numbers
- MongoDB query to group records and display a specific value with dot notation
- MongoDB Query to select records having a given key?
- MongoDB query to insert but limit the total records
- MongoDB query to get date records in a range
- MongoDB aggregation with multiple keys
- MongoDB query to find and return subdocument with criteria?
- Get the maximum mark records from a collection with documents in MongoDB query
- Find the records with % character in a LIKE query with MySQL
- MongoDB query to fetch date records (ISODate format) in a range
- MongoDB query to search date records using only Month and Day
- MongoDB query to count records on the basis of matching criteria

Advertisements