MONGOC_MATCHER(3)

NAME
mongoc_matcher - Client Side Document Matching

BASIC DOCUMENT MATCHING (DEPRECATED)

WARNING:
        This feature will be removed in version 2.0.

The MongoDB C driver supports matching a subset of the MongoDB query specification on the client.

Currently, basic numeric, string, subdocument, and array equality, $gt, $gte, $lt, $lte, $in, $nin, $ne, $exists, $type, $and, and $or are supported. As this is not the same implementation as the MongoDB server, some inconsistencies may occur. Please file a bug if you find such a case.

The following example performs a basic query against a BSON document. example-matcher.c.INDENT 0.0

    #include <bcon.h>
    #include <mongoc.h>
    #include <stdio.h>

    static void
    log_query (const bson_t *doc, const bson_t *query)
    {
       char *str1;
       char *str2;

       str1 = bson_as_canonical_extended_json (doc, NULL);
       str2 = bson_as_canonical_extended_json (query, NULL);

       printf ("Matching %s against %s\n", str2, str1);

       bson_free (str1);
       bson_free (str2);
    }

    static void
    check_match (const bson_t *doc, const bson_t *query)
    {
       bson_error_t error;
       mongoc_matcher_t *matcher = mongoc_matcher_new (query, &error);
       if (!matcher) {
          fprintf (stderr, "Error: %s\n", error.message);
          return;
       }

       if (mongoc_matcher_match (matcher, doc)) {
          printf ("  Document matched!\n");
       } else {
          printf ("  No match.\n");
       }

       mongoc_matcher_destroy (matcher);
    }

    static void
    example (void)
    {
       bson_t *query;
       bson_t *doc;

       doc = BCON_NEW ("hello", "[", "{", "foo", BCON_UTF8 ("bar"), "}", "]");
       query = BCON_NEW ("hello.0.foo", BCON_UTF8 ("bar"));

       log_query (doc, query);
       check_match (doc, query);

       bson_destroy (doc);
       bson_destroy (query);

       /* i is > 1 or i < -1. */
       query = BCON_NEW ("$or",
                         "[",
                         "{",
                         "i",
                         "{",
                         "$gt",
                         BCON_INT32 (1),
                         "}",
                         "}",
                         "{",
                         "i",
                         "{",
                         "$lt",
                         BCON_INT32 (-1),
                         "}",
                         "}",
                         "]");

       doc = BCON_NEW ("i", BCON_INT32 (2));
       log_query (doc, query);
       check_match (doc, query);

       bson_destroy (doc);

       doc = BCON_NEW ("i", BCON_INT32 (0));
       log_query (doc, query);
       check_match (doc, query);

       bson_destroy (doc);
       bson_destroy (query);
    }

    int
    main (int argc, char *argv[])
    {
       mongoc_init ();
       example ();
       mongoc_cleanup ();

       return 0;
    }

The following example shows how to process a BSON stream from stdin and match it against a query. This can be useful if you need to perform simple matching against mongodump backups. filter-bsondump.c.INDENT 0.0

    /*
     * Copyright 2014 MongoDB, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *   http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    #include <bcon.h>
    #include <bson.h>
    #include <mongoc.h>
    #include <stdio.h>

    /*
     * This is an example that reads BSON documents from STDIN and prints them
     * to standard output as JSON if they match {'hello': 'world'}.
     */

    int
    main (int argc, char *argv[])
    {
       mongoc_matcher_t *matcher;
       bson_reader_t *reader;
       const bson_t *bson;
       bson_t *spec;
       char *str;
       int fd;

       mongoc_init ();

    #ifdef _WIN32
       fd = fileno (stdin);
    #else
       fd = STDIN_FILENO;
    #endif

       reader = bson_reader_new_from_fd (fd, false);

       spec = BCON_NEW ("hello", "world");
       matcher = mongoc_matcher_new (spec, NULL);

       while ((bson = bson_reader_read (reader, NULL))) {
          if (mongoc_matcher_match (matcher, bson)) {
             str = bson_as_canonical_extended_json (bson, NULL);
             printf ("%s\n", str);
             bson_free (str);
          }
       }

       bson_reader_destroy (reader);
       bson_destroy (spec);

       return 0;
    }

To test this, perform a mongodump of a single collection and pipe it to the program.

        $ echo "db.test.insert({hello:'world'})" | mongo
        MongoDB shell version: 2.6.1
        connecting to: test
        WriteResult({ "nInserted" : 1 })
        bye

        $ mongodump -d test -c test -o - | filter-bsondump
        { "_id" : { "$oid" : "537afac9a70e5b4d556153bc" }, "hello" : "world" }

AUTHOR
MongoDB, Inc

COPYRIGHT
2018, MongoDB, Inc