Posts

Showing posts with the label Mongodb

Auto Increment In MongoDB To Store Sequence Of Unique User ID

Answer : As selected answer says you can use findAndModify to generate sequential IDs. But I strongly disagree with opinion that you should not do that. It all depends on your business needs. Having 12-byte ID may be very resource consuming and cause significant scalability issues in future. I have detailed answer here . You can, but you should not https://web.archive.org/web/20151009224806/http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ Each object in mongo already has an id, and they are sortable in insertion order. What is wrong with getting collection of user objects, iterating over it and use this as incremented ID? Er go for kind of map-reduce job entirely I know this is an old question, but I shall post my answer for posterity... It depends on the system that you are building and the particular business rules in place. I am building a moderate to large scale CRM in MongoDb, C# (Backend API), and Angular (Frontend web app) and fou...

Aggregation With Update In MongoDB

Answer : After a lot of trouble, experimenting mongo shell I've finally got a solution to my question. Psudocode: # To get the list of customer whose score is greater than 2000 cust_to_clear=db.col.aggregate( {$match:{$or:[{status:'A'},{status:'B'}]}}, {$group:{_id:'$cust_id',total:{$sum:'$score'}}}, {$match:{total:{$gt:500}}}) # To loop through the result fetched from above code and update the clear cust_to_clear.result.forEach ( function(x) { db.col.update({cust_id:x._id},{$set:{clear:'Yes'}},{multi:true}); } ) Please comment, if you have any different solution for the same question. With Mongo 4.2 it is now possible to do this using update with aggregation pipeline. The example 2 has example how you do conditional updates: db.runCommand( { update: "students", updates: [ { q: { }, u: [ { $set: { average : { $avg: "$tests...

Aggregate/project Sub-document As Top-level Document In Mongo

Answer : When you have many, many fields in the sub-document and occasionally it is updated with new fields, then projection is not a viable option. Fortunately, since 3.4, MongoDB has a new operator called $replaceRoot . All you have to do is add a new stage at the end of your pipeline. db.getCollection('sample').aggregate([ { $replaceRoot: {newRoot: "$command"} }, { $project: {score: 0 } //exclude score field } ]) This would give you the desired output. Note that in case of aggregation (especially after a $group stage) the 'command' document could be an array and could contain multiple documents. In this case you need to $unwind the array first to be able to use $replaceRoot . As you have guessed, $project allows you to do that: db.col.aggregate([ { $project : { _id: "$command._id", name: "$command.name", strike: "$command.strike", duratio...

Bulk Upsert In MongoDB Using Mongoose

Answer : Not in "mongoose" specifically, or at least not yet as of writing. The MongoDB shell as of the 2.6 release actually uses the "Bulk operations API" "under the hood" as it were for all of the general helper methods. In it's implementation, it tries to do this first, and if an older version server is detected then there is a "fallback" to the legacy implementation. All of the mongoose methods "currently" use the "legacy" implementation or the write concern response and the basic legacy methods. But there is a .collection accessor from any given mongoose model that essentially accesses the "collection object" from the underlying "node native driver" on which mongoose is implemented itself: var mongoose = require('mongoose'), Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/test'); var sampleSchema = new Schema({},{ "strict": false }); va...

Authentication Failed When Using Flask_pymongo

Answer : This question may be old, but I was experiencing the same issue and found another solution that might work for others. Appending ?authSource=admin to the end of your MONGO_URI variable will authenticate your credentials against the admin database, rather than the one you're connecting to. Example: app.config["MONGO_URI"] = "mongodb://username:password@host:port/db_name?authSource=admin" The problem is if you use the MONGO_URI config parameter then pymongo attempts to authenticate against the db name included in the string. You should break up the config parameters into the following so you can specify a db name and an auth source. app.config['MONGO_HOST'] = 'localhost' app.config['MONGO_PORT'] = '27017' app.config['MONGO_DBNAME'] = 'mongo_test' app.config['MONGO_USERNAME'] = 'root' app.config['MONGO_PASSWORD'] = 'aaa2016' app.config['MONGO_AUTH_SOURCE']...

Can Mongo Upsert Array Data?

Answer : I'm not aware of an option that would upsert into an embedded array as at MongoDB 2.2, so you will likely have to handle this in your application code. Given that you want to treat the embedded array as sort of a virtual collection, you may want to consider modelling the array as a separate collection instead. You can't do an upsert based on a field value within an embedded array, but you could use $addToSet to insert an embedded document if it doesn't exist already: db.soup.update({ "tester":"tom" }, { $addToSet: { 'array': { "id": "3", "letter": "d" } } }) That doesn't fit your exact use case of matching by id of the array element, but may be useful if you know the expected current value. I just ran into this problem myself. I wasn't able to find a one-call solution, but I found a two-call solution that works when you have ...

Auto Populate Date In MongoDB On Insert

Answer : You may try to do a few things if you do not want to handle this from code (I have executed the code below directly on mongo shell): If you want to use $currentDate use update with upsert = true: db.orders.update( {"_id":ObjectId()}, { $currentDate: { createtime: true } }, { upsert: true } ) It will now generate the objectid on app server instead of date/time (unless you use raw command). Use new timestamp or date object directly: db.orders.insert( "createtime": new Timestamp() ) The problem with most driver will be then to make sure the new object is created on mondodb server- not on the machine where the code is running. You driver hopefully allows to run raw insert command. Both will serve the purpose of avoiding time differences/ time sync issue between application server machines.

C# MongoDB Distinct Query Syntax

Answer : You could try the following approach: var filter = new BsonDocument(); var categoriesList = await blogContext.Articles.DistinctAsync<string>("categories", filter);

Bash: Mongo: Command Not Found

Answer : The problem can solve if you add MongoDB path in the system variables: Go to this path " Control Panel\System and Security\System " on the left you should see some options click on " Advanced system settings " after this click on " Environment Variables " at the System variables should be one option " Path " click that option and " Edit " it, add " New " variable on this system, in my case, is " C:\Program Files\MongoDB\Server\4.2\bin " and save this change. if you're using git bash, use the following command $ ./mongo I've just had the same problem and did this to solve it. I hope it helps (late, but to be sure it is answered). Open GitBash and type the following: 1) cd.. // to go to the root directory. 2) touch .bash_profile // creates a hidden file to set up the shortcut 3) notepad .bash_profile // this opens that hidden file with notepad. You can also do it with VIM if you want t...

Bulk Update In Pymongo Using Multiple ObjectId

Answer : Iterate through the id list using a for loop and send the bulk updates in batches of 500: bulk = db.testdata.initialize_unordered_bulk_op() counter = 0 for id in ids: # process in bulk bulk.find({ '_id': id }).update({ '$set': { 'isBad': 'N' } }) counter += 1 if (counter % 500 == 0): bulk.execute() bulk = db.testdata.initialize_ordered_bulk_op() if (counter % 500 != 0): bulk.execute() Because write commands can accept no more than 1000 operations (from the docs ), you will have to split bulk operations into multiple batches, in this case you can choose an arbitrary batch size of up to 1000. The reason for choosing 500 is to ensure that the sum of the associated document from the Bulk.find() and the update document is less than or equal to the maximum BSON document size even though there is no there is no guarantee using the default 1000 operations requests will fit under the 16MB BSON limit. Th...

Can A $text Search Perform A Partial Match

Answer : MongoDB $text searches do not support partial matching. MongoDB allows text search queries on string content with support for case insensitivity, delimiters, stop words and stemming. And the terms in your search string are, by default, OR'ed. Taking your (very useful :) examples one by one: SINGLE TERM, PARTIAL // returns nothing because there is no world word with the value `Crai` in your // text index and there is no whole word for which `Crai` is a recognised stem db.submissions.find({"$text":{"$search":"\"Crai\""}}) MULTIPLE TERMS, COMPLETE // returns the document because it contains all of these words // note in the text index Dr. Bob is not a single entry since "." is a delimiter db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bob\""}}) MULTIPLE TERMS, ONE PARTIAL // returns the document because it contains the whole word "Craig" an...

Can't Use Mongo Command, Shows Command Not Found On Mac

Answer : You need to add the path to "mongo" to your terminal shell. export PATH=$PATH:/usr/local/mongodb/bin Did you do the last step with paths.d? If so, try restarting your terminals. Do you have a good reason for using 1.8.5? The current stable is 2.0.4, and it has many useful upgrades from 1.8.x You'll have to add the location of the Mongo binary to PATH. Follow the steps below in order to make the PATH variable permanent: Open Terminal and navigate to your user directory. Run touch ~/.bash_profile and then open ~/.bash_profile . In TextEdit, add export PATH="<mongo-directory>/bin:$PATH" (Keep the quote marks - related to white spaces). Save the .bash_profile file and Quit (Command + Q) Text Edit. Run source ~/.bash_profile . Run echo $PATH and check if the you see that the Mongo binary was added. (*) Notice that the PATH variable is now available only for the current terminal and not to processes that were already started...