How to check if a collection exists in Mongodb native nodejs driver?

The collectionNames method of the native driver’s Db object accepts an optional collection name filter as a first parameter to let you check the existence of a collection: db.collectionNames(collName, function(err, names) { console.log(‘Exists: ‘, names.length > 0); }); In the 2.x version of the MongoDB native driver, collectionNames has been replaced by listCollections which accepts …

Read more

Why Is MongoDB So Fast

MongoDB is fast because its web scale! Its a fun video and well worth everyone watching, but it does answer your question – that most of the noSQL engines like MongoDB are not robust and not resilient to crashes and other outages. This security is what they sacrifice to gain speed.

how to show query while using query annotations with MongoRepository with spring data

I add the line (below) in and works fine: for query: @Query(“{$and: [{‘$or’ : [{ ‘name’: {$regex : ?0, $options: ‘i’}}, {‘description’: {$regex : ?1, $options: ‘i’}}]}, { ‘deleted’ : ?2 }]}”) obtain this log: 2016-09-27 10:53:26.245 DEBUG 13604 — [nio-9090-exec-3] : find using query: { “$and” : [ { “$or” : …

Read more

Creating a Foreign Key relationship in Mongoose

Check Updated code below, in particular this part: {type: Schema.Types.ObjectId, ref: ‘Ingredient’} var mongoose = require(‘mongoose’); var Schema = mongoose.Schema; var IngredientSchema = new Schema({ name: String }); module.exports = mongoose.model(‘Ingredient’, IngredientSchema); var mongoose = require(‘mongoose’); var Schema = mongoose.Schema; var RecipeSchema = new Schema({ name: String, ingredients:[ {type: Schema.Types.ObjectId, ref: ‘Ingredient’} ] }); module.exports …

Read more

In MongoDB’s pymongo, how do I do a count()?

Since pymongo version 3.7.0 and above count() is deprecated. Instead use Collection.count_documents. Running cursor.count or collection.count will result in following warning message: DeprecationWarning: count is deprecated. Use Collection.count_documents instead. To use count_documents the code can be adjusted as follows import pymongo db = pymongo.MongoClient() col = db[DATABASE][COLLECTION] find = {“test_set”:”abc”} sort = [(“abc”,pymongo.DESCENDING)] skip = …

Read more

MongoDB: count the number of items in an array

In MongoDB 2.6, the Aggregation Framework has a new array $size operator you can use: > db.mycollection.insert({‘foo’:[1,2,3,4]}) > db.mycollection.insert({‘foo’:[5,6,7]}) > db.mycollection.aggregate([{$project: { count: { $size:”$foo” }}}]) { “_id” : ObjectId(“5314b5c360477752b449eedf”), “count” : 4 } { “_id” : ObjectId(“5314b5c860477752b449eee0”), “count” : 3 }