[SERVER-27774] Add $of for iterating over object keys Created: 21/Jan/17  Updated: 06/Dec/22  Resolved: 26/Jan/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: daniel.morrison#1 Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-267 Wildcard support in index/query/proje... Backlog
Related
is related to SERVER-18794 Add an aggregation expression to conv... Closed
Assigned Teams:
Query
Participants:

 Description   

In many languages you have the ability iterate over objects in the same way as you iterate arrays. In ES6 the simple example is for...of where its equivalent for array is for...in

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of

we already have $in for iterating our query across an array, could we add $of to do the same against objects in a document?



 Comments   
Comment by Charlie Swanson [ 26/Jan/17 ]

Hi daniel.morrison,

After reviewing your request, it seems like the requested functionality has already been requested in SERVER-267. If I understand the request correctly, implementing SERVER-267 would solve your problem. Because of this, I'm going to close this ticket as a duplicate. Please vote for SERVER-267 and watch it for further updates. If I missed something, and this is not actually a duplicate, please leave a comment describing the differences, and we can re-open the ticket.

I would also like to point you to the related ticket SERVER-18794, which proposes a way to turn the object into an array, which could then be queried over more easily.

Thank you for the request,
Charlie

Comment by Kelsey Schubert [ 23/Jan/17 ]

Hi daniel.morrison,

Thanks for the clarifying examples. I'm sending this feature request to the Query Team for consideration.

Kind regards,
Thomas

Comment by daniel.morrison#1 [ 21/Jan/17 ]

Equivalent in ES6

const daniel = { _id: 1, name: "Daniel", days: { monday: { hours:8, sick: false }, tuesday: { hours:8, sick: false }, wednesday: { hours:0, sick: true} } } 
for (let day in daniel.days) {
  if (daniel.days[day].sick === true) {
    return true;
  }
}

Comment by daniel.morrison#1 [ 21/Jan/17 ]

{ field: { $in: { key: value } } }

collection:

{ _id: 1, name: "Daniel", days: { monday: { hours:8, sick: false }, tuesday: { hours:8, sick: false }, wednesday: { hours:0, sick: true} } } 
{ _id: 2, item: "Ramon", days: { monday: { hours:8, sick: false }, tuesday: { hours:8, sick: false }, wednesday: { hours:8, sick: false} } } 
{ _id: 2, item: "Paul", days: { monday: { hours:0, sick: true }, tuesday: { hours:8, sick: false }, wednesday: { hours:8, sick: false} } } 

query:

db.collection.find(
  {days: {$in: { sick: true } } }
)

result:

[
  { _id: 1, name: "Daniel", days: { monday: { hours:8, sick: false }, tuesday: { hours:8, sick: false }, wednesday: { hours:0, sick: true} } } 
  { _id: 2, item: "Paul", days: { monday: { hours:0, sick: true }, tuesday: { hours:8, sick: false }, wednesday: { hours:8, sick: false} } } 
]

@ramon.fernandez does this look ok?

Comment by Ramon Fernandez Marina [ 21/Jan/17 ]

Hi daniel.morrison, could you please provide a sample syntax for this new operator and further details of your use case?

Thanks,
Ramón.

Generated at Thu Feb 08 04:16:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.