How do I query for distinct values in Mongoose?
Just to give an update for Mongoose 3.x: MyModel.find().distinct(‘_id’, function(error, ids) { // ids is an array of all ObjectIds });
Just to give an update for Mongoose 3.x: MyModel.find().distinct(‘_id’, function(error, ids) { // ids is an array of all ObjectIds });
Sum To get the sum of a grouped field when using the Aggregation Framework of MongoDB, you’ll need to use $group and $sum: db.characters.aggregate([ { $group: { _id: null, total: { $sum: “$wins” } } } ] ) In this case, if you want to get the sum of all of the wins, you need … Read more
This is the main error: exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating Catalina has a surprise change: it won’t allow changes to the root directory (this was discussed in a forum thread as well): % sudo mkdir -p /data/db mkdir: /data/db: Read-only file system Unfortunately, this is not spelled out explicitly in … Read more
Using skip+limit is not a good way to do paging when performance is an issue, or with large collections; it will get slower and slower as you increase the page number. Using skip requires the server to walk though all the documents (or index values) from 0 to the offset (skip) value. It is much … Read more
Using $nin will work, but you have the syntax wrong. It should be: db.collection.find({‘docs.foo’: {$nin: [1]}})
After converting the strings into ObjectIds, you can use the $in operator to get the docs in the list. There isn’t any query notation to get the docs back in the order of your list, but see here for some ways to handle that. var ids = [‘512d5793abb900bf3e20d012’, ‘512d5793abb900bf3e20d011’]; var obj_ids = ids.map(function(id) { return … Read more
mongorestore will only drop the existing collection if you use the –drop argument. If you don’t use –drop, all documents will be inserted into the existing collection, unless a document with the same _id already exists. Documents with the same _id will be skipped, they are not merged. So mongorestore will never delete or modify … Read more
To truncate a collection and keep the indexes use db.<collection>.remove({})
You need to actually point at the “database name” container directory “within” the output directory from the previous dump: mongorestore -d db2 dumpdir/db1 And usually just <path> is fine as a positional argument rather than with -dir which would only be needed when “out of position” i.e “in the middle of the arguments list”. p.s. … Read more
From the Mongoose 6.0 docs: useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options. Mongoose 6 always behaves as if useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false. Please remove these options from your code.