0

I'm trying to implement field level redaction as described in the MongoDB tutorial pages in a Java application.

I'm recieving an error that "both operands of $setIsSubset must be arrays. First argument is of type: string" when the first operand is a $$ reference set in the previous map document. The mapping does seem to work, as when I target a result without the relevant tags I get an expected "object is null" error from the anyElementTrue document.

List<String> userAccess = Arrays.asList("Delta");

    List<Document> results = collection.aggregate(Arrays.asList(
            match(eq("_id", new ObjectId(id))),
            new Document("$redact", 
              new Document("$cond", Arrays.asList(
                new Document("$anyElementTrue",
                  new Document("$map",  new Document("input", "$tags")
                    .append("as", "objectAccess")
                    .append("in",
                      new Document("$setIsSubset", Arrays.asList("$$objectAccess", userAccess)))))
            ,"$$KEEP", "$$PRUNE" )))
    )).into(new ArrayList<>());

The stored object includes a tags array;

{"_id":{"$oid":"5e9d755207ca65177de4d5ba"},
  "name":"Sydney Breakfast",
  "notes":"An average tea",
  "rating":{"$numberInt":"3"},
  "isCaffeine":true,
  "tags":["Alpha","Bravo"]}
Jontia
  • 137
  • 14
  • _"I'm recieving an error that "both operands of $setIsSubset must be arrays."_ You are getting the error because the reference to `"objectAccess"`_must_ be an array. Is the element of the array `tags` a nested array? You can post a sample input document. – prasad_ Apr 21 '20 at 04:29
  • @prasad_ is that the sort of thing you were looking for. The tags objecct is itself an array, not a nested array. – Jontia Apr 21 '20 at 08:45
  • 1
    Consider this code snippet: `new Document("$setIsSubset", Arrays.asList("$$objectAccess", userAccess))`. The `"$$objectAccess"` and `userAccess` are the arguments to the [$setIsSubset](https://docs.mongodb.com/manual/reference/operator/aggregation/setIsSubset/index.html) aggregation operator. From the above code the variable `"$$objectAccess"` is derived from the [$map](https://docs.mongodb.com/manual/reference/operator/aggregation/map/index.html) operator. $map documentation says the field defined with the `as` of $map is an array element (not an array). Thats the reason of the error. – prasad_ Apr 21 '20 at 09:01
  • 1
    The link you had posted [MongoDB tutorial for field level redaction](https://docs.mongodb.com/manual/tutorial/implement-field-level-redaction/) uses similar example with the `tags` array field. But, in _that_ example the elements of the `tags` are also arrays: `tags: [ [ "HCS"], [ "FDW", "TGE", "BX" ] ]`. But, in your document the `tags` field is: `["Alpha","Bravo"]`. Note the difference. That is the reason of the error you are getting. – prasad_ Apr 21 '20 at 09:09
  • @prasad_ ah! Fantastic thank you. If you want to write that up as an answer I can accept it. Turning tags into a nested array solved this immediately. It was right there on the page, can't believe I couldn't see it. Very embarassing. – Jontia Apr 21 '20 at 09:10

0 Answers0