Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 205 Vote(s) - 3.32 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to search in array of object in mongodb

#1
Suppose the mongodb document(table) 'users' is

{
_id: 1,
name: {
first: 'John',
last: 'Backus'
},
birth: new Date('Dec 03, 1924'),
death: new Date('Mar 17, 2007'),
contribs: ['Fortran', 'ALGOL', 'Backus-Naur Form', 'FP'],
awards: [
{
award: 'National Medal',
year: 1975,
by: 'NSF'
},
{
award: 'Turing Award',
year: 1977,
by: 'ACM'
}
]
}
// ...and other object(person)s

I want to find the person who has the award 'National Medal' and must be awarded in year 1975
There could be other persons who have this award in different years.

How can I find this person using award type and year. So I can get exact person.
Reply

#2
You can do this in two ways:

1. ElementMatch - `$elemMatch` (as explained in above answers)

`db.users.find({ awards: { $elemMatch: {award:'Turing Award', year:1977} } })`

2. Use `$and` with `find`

`db.getCollection('users').find({"$and":[{"awards.award":"Turing Award"},{"awards.year":1977}]})`
Reply

#3
Use `$elemMatch` to find the array of a particular object

db.users.findOne({"_id": id},{awards: {$elemMatch: {award:'Turing Award', year:1977}}})

Reply

#4
as explained in above answers Also, to return only one field from the entire array you can use [`projection`](

[To see links please register here]

) into find. and use `$`
```
db.getCollection("sizer").find(
{ awards: { $elemMatch: { award: "National Medal", year: 1975 } } },
{ "awards.$": 1, name: 1 }
);
```
will be return
```
{
_id: 1,
name: {
first: 'John',
last: 'Backus'
},
awards: [
{
award: 'National Medal',
year: 1975,
by: 'NSF'
}
]
}
Reply

#5
The right way is:

db.users.find({awards: {$elemMatch: {award:'National Medal', year:1975}}})

`$elemMatch` allows you to match more than one component within the same array element.

Without `$elemMatch` mongo will look for users with National Medal in some year and some award in the year 1975, but not for users with National Medal in 1975.

See [MongoDB $elemMatch Documentation](

[To see links please register here]

) for more info. See [Read Operations Documentation](

[To see links please register here]

) for more information about querying documents with arrays.
Reply

#6
Use $elemMatch to find the array of a particular object
doc:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through