Skip to content

Instantly share code, notes, and snippets.

@vhbui02
Last active May 17, 2023 11:46
Show Gist options
  • Save vhbui02/bfa1b03648d4a1da7c8d7d4281b41197 to your computer and use it in GitHub Desktop.
Save vhbui02/bfa1b03648d4a1da7c8d7d4281b41197 to your computer and use it in GitHub Desktop.
[MongoDB R operation]

Get list of Collection

db.getCollectionInfos() db.getCollectionNames()

MAIN METHOD

db.collection.find()

MATCH NORMAL DATA TYPE

Match Embedded/Nested/Sub Document inside a Document

db.inventory.find(
  // filter condition
  {
    size: {
      h: 14,
      w: 21,
      uom: 'cm'

      // won't match any docs
      // uom: 'cm'
      // w: 21,
      // h: 14,
    }, 
    'some field that need to be existed': {
      $exists: true
    }
    'some text field': {
    $regex: /^bc/,
  }, 
  // or 'some text field': /^bc/
  }, 
  // projection paramter
  // use projection operator here
  {
    size: 1,
    'some field that you want to show': 1
    '_id': 0 
    // usually _id since it always show defaultly, the only way to not show it is set it to 0, redundant if used with fields other than _id
  }
)

Note: Field Order Matters

Match Embedded Field inside Embedded Document

Using 'dot notation'

Note: in 'dot notation', the FIELD and NESTED FIELD must be inside quotation marks

Match by "equal"

db.inventory.find({
	'size.uom': 'inch'
})

Match by "filter"

// using comparison operator
db.inventory.find({
  // implicit AND between 'size.h' and 'size.w'
  'size.h': {
    // implicit AND between $gt and $lt
    $gt: 25,
    $lt: 30,
  },
  'size.w': {
    $gt: 30
  }

  // $eq - equilvalent to direct way. why people still use it? idk
  // $lte
  // $gte
  // $in
  // $ne 
  // $nin 
})

// using logical operator
// remember, it wrapped OUTSIDE the field values
use('sample_restaurants')
db.restaurants
   .find({
      $and: [
         {
           	borough: {
              	// implicit AND between $exists and $eq
              	$exists: true,
            	$eq: 'Brooklyn' // now i understand why $eq operator here
            }
         },
         {
            $or: [
               {
                  cuisine: 'Italian',
               },
               {
                  cuisine: 'Caribbean',
               },
            ],
         },
      ],
   })

QUERY AN ARRAY

Match 'fully' or Match 'partially'

// match 'fully', order matter
find({tags: ["red", "blank"]})

// match 'partially', no order
find({
  	tags: {
     	$all: ['red', 'blank']
	}
})

Match single element

// no condition
find({
	tags: "red" // NOTE: no square brackets [] here
})

Match element by filter condition

// all element meets multiple criteria
find({
  dim_cm: {
    $gt: 15,
    $lt: 20
  }
})

// at least 1 element meets multiple criteria
find({
  dim_cm: {
    $elemMatch: {
      $gt: 15,
      $lt: 20
    }
  }
})

// array index position matter
// array use zero-based indexing
find({
  // 2nd element
  'dim_cm.1': {
    $gt: 25
  }
})

// array length
find({
  dim_cm: {
    $size: 3
  }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment