[SERVER-30196] $size for objects (implementing update-if-current/atomics) Created: 17/Jul/17  Updated: 24/Aug/17  Resolved: 28/Jul/17

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

Type: Improvement Priority: Major - P3
Reporter: Karolin Varner Assignee: Asya Kamsky
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-12199 Add an operator that matches an entir... Backlog
Participants:

 Description   

It would be nice if the $size operator supported objects!

This would specifically be handy for implementing the update-if-current idom; e.g. one could match the following subdocument exactly and order-indipentantly:

Document:

{
  _id: 1,
  my_doc: {
    name: "Ford Prefect",
    occupation: "Writer"
  }
}

Query:

{
  _id: 1,
  "my_doc.name": "Ford Prefect",
  "my_doc.occupation": "Writer",
  my_doc: {$size: 2}
}



 Comments   
Comment by Asya Kamsky [ 28/Jul/17 ]

Because it will be possible to do this in several different ways in the upcoming 3.6 release, either via aggregation expression or via $jsonSchema operator, it's unlikely we would also add a top level operator for number of fields in an object, so I'm closing this ticket as Won't Fix.

Comment by Asya Kamsky [ 21/Jul/17 ]

Note that this can also be achieved via aggregation expressions:

db.test.aggregate([
   {$match:{_id: 1, "my_doc.name": "Ford Prefect", "my_doc.occupation": "Writer",}}, 
   {$addFields:{__numFields:{$size: {$objectToArray:"$doc"}}}},
   {$match:{__numFields:2}}
])

Comment by David Storch [ 21/Jul/17 ]

Hi karo, this looks like something that can be expressed with JSON Schema, albeit in a somewhat wordy fashion. The work to add support for JSON Schema is being tracked by SERVER-30191. JSON Schema has maxProperties and minProperties keywords, which would allow you to write your query like so:

db.collection.find({
    _id: 1,
    "my_doc.name": "Ford Prefect",
    "my_doc.occupation": "Writer",
    $jsonSchema: {type: "object", properties: {my_doc: {minProperties: 2, maxProperties: 2}}}
});

However, I think it still makes sense to leave this feature request open, since we may still want to add querying on the number of properties in an object as a first-class citizen of the MongoDB query language.

Best,
Dave

Comment by Karolin Varner [ 20/Jul/17 ]

Hi Mark,
absolutely! You got it!

That's quite all right, thanks for responding a second time! This is ticket 30196 so I guess you have a fairly intense influx of tickets that need quick closing

Best,
Karolin

Comment by Mark Agarunov [ 20/Jul/17 ]

Hello karo,

My apologies, I initially misunderstood your request. To clarify the feature you're requesting, my understanding is that you are proposing an operator that will allow for counting the number of fields in an object, regardless of their values or order. Please let me know if that describes the behavior you are requesting correctly.

Thanks,
Mark

Comment by Karolin Varner [ 20/Jul/17 ]

@Mark This was a feature request btw. I tested and I was pretty sure that the functionality I am suggesting in here does not exist in mongodb yet, which is why I didn't post it on stackoverflow.

Comment by Karolin Varner [ 20/Jul/17 ]

Well, I need to be able to use it in a query if I want to use it as a filter clause in update() (to implement update-if-current).
I could of course filter documents returned by find() using bsonSize after I retrieved them.

Comment by Mark Agarunov [ 19/Jul/17 ]

Hello karo,

Thank you for the report. Looking over the examples you've provided, the Object.bsonsize function may work for your use case. For example:

 
> db.test.insert({
   _id: 1,
   my_doc: {
      name: "Ford Prefect",
      occupation: "Writer"
   }
})
WriteResult({ "nInserted" : 1 }) 
> db.test.insert({
   _id: 2,   
   my_doc: {              
      occupation: "Writer",
      name: "Ford Prefect"   
   }    
})
WriteResult({ "nInserted" : 1 }) 
> Object.bsonsize(db.test.find({_id:1}))
1099
> Object.bsonsize(db.test.find({_id:2}))
1099

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-user group.

Thanks,
Mark

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