Monthly Archives

September 2021

MongoDB | Read Operation with Arrays

By | MongoDB Learning
db.MoviesCollection.find({genres: {$size: 3}})

Find all generes which has size of 3 elements/documents


db.movieStarts.find({genre: { $all: ["action", "thriller"]}}, {title: 1, genre: 1, _id:0 })

Find all generes having action and thriller element. If $all is not used, it will search for the particular order and which is not what we might want.


db.users.find({ hobbies: { $elemMatch: { title:  'sports', frequency: { $gte:  3 } } } }).pretty()

Find all hobbies with element having sports and frequency Greater than or eqals to 3.

MongoDB | Query and Projections

By | MongoDB Learning

Query Selectors

Comparison

For comparison of different BSON type values, see the specified BSON comparison order.

NameDescription
$eqMatches values that are equal to a specified value.
$gtMatches values that are greater than a specified value.
$gteMatches values that are greater than or equal to a specified value.
$inMatches any of the values specified in an array.
$ltMatches values that are less than a specified value.
$lteMatches values that are less than or equal to a specified value.
$neMatches all values that are not equal to a specified value.
$ninMatches none of the values specified in an array.

Logical

NameDescription
$andJoins query clauses with a logical AND returns all documents that match the conditions of both clauses.
$notInverts the effect of a query expression and returns documents that do not match the query expression.
$norJoins query clauses with a logical NOR returns all documents that fail to match both clauses.
$orJoins query clauses with a logical OR returns all documents that match the conditions of either clause.

Element

NameDescription
$existsMatches documents that have the specified field.
$typeSelects documents if a field is of the specified type.

Evaluation

NameDescription
$exprAllows use of aggregation expressions within the query language.
$jsonSchemaValidate documents against the given JSON Schema.
$modPerforms a modulo operation on the value of a field and selects documents with a specified result.
$regexSelects documents where values match a specified regular expression.
$textPerforms text search.
$whereMatches documents that satisfy a JavaScript expression.

Geospatial

NameDescription
$geoIntersectsSelects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
$geoWithinSelects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
$nearReturns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
$nearSphereReturns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.

Array

NameDescription
$allMatches arrays that contain all elements specified in the query.
$elemMatchSelects documents if element in the array field matches all the specified $elemMatch conditions.
$sizeSelects documents if the array field is a specified size.

Bitwise

NameDescription
$bitsAllClearMatches numeric or binary values in which a set of bit positions all have a value of 0.
$bitsAllSetMatches numeric or binary values in which a set of bit positions all have a value of 1.
$bitsAnyClearMatches numeric or binary values in which any bit from a set of bit positions has a value of 0.
$bitsAnySetMatches numeric or binary values in which any bit from a set of bit positions has a value of 1.

Comments

NameDescription
$commentAdds a comment to a query predicate.

Projection Operators

NameDescription
$Projects the first element in an array that matches the query condition.
$elemMatchProjects the first element in an array that matches the specified $elemMatch condition.
$metaProjects the document’s score assigned during $text operation.
$sliceLimits the number of elements projected from an array. Supports skip and limit slices.

MongoDB | Exploring Insert Option

By | MongoDB Learning
db.tableName.insertMany([{_id: 1, name: 1}, {_id: 2, name: 2}, {_id: 3, name: 3}])

This will insert documents. In case let assume name:2 is already in database, it will insert name:1 and throw error. the name: 3 won't be inserted in default behaviour. To get new document inserted and skip the already inserted documents, we can use:

db.tableName.insertMany([{_id: 1, name: 1}, {_id: 2, name: 2}, {_id: 3, name: 3}], {ordered: false})

The argument ordered: false will insert document which are not present in the document. By default the ordered is set to true.


mongoimport Downloads/tv-shows.json -d MoviesData -c MoviesCollection --jsonArray

import document from json file, -d creates database if not exists, -c create collection if not exsist, --jsonArray if the document contain array of documents.


https://docs.mongodb.com/manual/reference/write-concern/