Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

How do I query for the value of a key in an object? [duplicate]

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Atul Kasana

User

( 8 months ago )

 

This question already has an answer here:

all the documentation on this subject seems aimed at retrieving elements from an array stored in mongodb. I have a document:

{
   "user1": { ... },
   "user2": { ... },
   ...
}

and want to retrieve the object stored for user1 but cannot find a way to formulate the query:

collection.find({user1})

won't work because it's destructuring of a variable that doesn't exist

var q = {}; q.user1 = '';
collection.find(q)

also wouldn't work because it would be looking for '' as the value.

how is this done??

and no, please don't post that I need to restructure my data as an array or in some other way. if it can't be done, just say so. thanks!

usr_profile.png
Priya Roy

User

( 8 months ago )

Have you tried?

const value = q['user1']

There is more here https://www.w3schools.com/js/js_json_objects.asp

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif