Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-67715

Change stream reader requires double escaping regexes

    • Fully Compatible
    • v6.1, v6.0
    • QE 2022-09-19, QE 2022-10-03

      We can open a change stream to watch events from non-system collections in a database, by using the regular expression "^system\." - starts with "system", and then an escaped .. All code below run on mongo shell connecting to single node replica set.

      > t = db.watch([{$match: {"ns.coll": {$nin: [/^system\./]}}}])
      

      However, this is NOT returning events from collections that start with "system" but are not actually system collections.

      Strangely, the same regex when used in a list collections (or aggregate with $listCatalog) to show all non-system collections will work correctly ("system_js" is printed):

      rs:PRIMARY> db.runCommand({listCollections: 1, filter: {name: {$nin: [/^system\./]}}})
      {
      	"cursor" : {
      		"id" : NumberLong(0),
      		"ns" : "test.$cmd.listCollections",
      		"firstBatch" : [
      			{
      				"name" : "system_js",
      				"type" : "collection",
      				"options" : {
      					
      				},
      				"info" : {
      					"readOnly" : false,
      					"uuid" : UUID("28c72e5c-204a-4943-b08c-b101413a2ebc")
      				},
      				"idIndex" : {
      					"v" : 2,
      					"key" : {
      						"_id" : 1
      					},
      					"name" : "_id_"
      				}
      			}
      		]
      	},
      	"ok" : 1,
      	"$clusterTime" : {
      		"clusterTime" : Timestamp(1656618172, 2),
      		"signature" : {
      			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
      			"keyId" : NumberLong(0)
      		}
      	},
      	"operationTime" : Timestamp(1656618172, 2)
      }
      

      In the change stream case, it seems like we have to escape the backslash as well and use "^system
      ." instead.

      > t = db.watch([{$match: {"ns.coll": {$nin: [/^system\\./]}}}])
      > t
      { "_id" : { "_data" : "8262BDF908000000012B042C0100296E5A1004CDB18D68D04F43BF868D66FC9DB28169463C6F7065726174696F6E54797065003C696E736572740046646F63756D656E744B65790046645F6964006462BDF908A431B84AA323A03E000004" }, "operationType" : "insert", "clusterTime" : Timestamp(1656617224, 1), "wallTime" : ISODate("2022-06-30T19:27:04.556Z"), "fullDocument" : { "_id" : ObjectId("62bdf908a431b84aa323a03e"), "b" : 1 }, "ns" : { "db" : "test", "coll" : "system_js" }, "documentKey" : { "_id" : ObjectId("62bdf908a431b84aa323a03e") } }
      

      See comments for more info.

            Assignee:
            kyle.suarez@mongodb.com Kyle Suarez
            Reporter:
            vishnu.kaushik@mongodb.com Vishnu Kaushik
            Votes:
            0 Vote for this issue
            Watchers:
            17 Start watching this issue

              Created:
              Updated:
              Resolved: