[SERVER-12199] Add an operator that matches an entire document Created: 24/Dec/13  Updated: 06/Dec/22

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

Type: New Feature Priority: Minor - P4
Reporter: Richard Kreuter (Inactive) Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-30196 $size for objects (implementing updat... Closed
Assigned Teams:
Query Optimization
Participants:

 Description   

Problem: it's occasionally desirable to be able to specify query that matches a whole document, but the query language doesn't support it (anyway, I can't think of one).

Proposal: add a query operator that matches if and only if a document has all the fields and no extra ones. For example, it might look like this:

db.foo.find({ "$whole" : { "_id" : 123 , "a" : "hello" , "b" : "world" } });

This would match only documents having the fields "_id", "a", and "b", but no other fields; and presumably it would match only if the document had those three fields in that order.

Note that this would make certain updates easier to express, e.g., for the update-if-current idiom.



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

Noting here that this is possible using aggregation by comparing "$$ROOT" and specified object. Similar to a workaround described in SERVER-30197:

db.foo.aggregate([
    {"$match" : { "_id" : 123 , "a" : "hello" , "b" : "world" } },
    {"$addFields" : {__keep:{$eq:[ {$literal: { "_id" : 123 , "a" : "hello" , "b" : "world" } }, "$$ROOT"]}}},
    {"$match" : {__keep:true}}
]);

Comment by Karolin Varner [ 18/Jul/17 ]

@David: Awesome. Thanks!

Comment by David Storch [ 18/Jul/17 ]

karo, assuming that you do not consider the ordering of the fields significant, this will be possible to express using JSON Schema. We are currently working on adding support for a $jsonSchema operator in the match language under SERVER-30191.

Comment by Karolin Varner [ 17/Jul/17 ]

If I am not mistaken it is still not possible to express this. For arrays we can use $size, but for objects there is no way to ensure that there are no extra values?

Generated at Thu Feb 08 03:27:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.