Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get keys of a document in MongoDB Document

Is there any way to get the list of keys of a particular document in MongoDB other than iterate through the document?

i.e I want to get the keys for the document returned by

db.users.find({username:'[email protected]})

Are there any inline commands.If not,can anybody give an idea on how to do?

like image 472
user1429322 Avatar asked Jun 19 '12 19:06

user1429322


People also ask

Are there keys in MongoDB?

All documents in a MongoDB collection have a primary key dubbed _id . This field is automatically assigned to a document upon insert, so there's rarely a need to provide it.

What are keys in MongoDB?

Key value databases, also known as key value stores, are database types where data is stored in a “key-value” format and optimized for reading and writing that data. The data is fetched by a unique key or a number of unique keys to retrieve the associated value with each key.

What does find () do in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.


1 Answers

Object.keys(db.users.findOne({username:'[email protected]'}))

will return a list of all the keys of a particular document.

like image 84
akki Avatar answered Sep 29 '22 01:09

akki