Mongoose compound index creation fields order - node.js

I have a question regarding compound index creation in mongodb:
Say I want to create this compound index:
cSchema.index({account:1, authorization: 1, c_type: 1});
Problem is, javascript doesn't guarantee dictionary order, so I won't be sure the compound index is in the order I want.
How can I make sure it's indeed {account:1, authorization:1, c_type:1} in that order ?
Thanks !

The simple answer is that most abuse the behaviour that simple String properties that don't parse to an integer on an Object will enumerate in creation order. Although not guaranteed in ES2015 for some enumeration methods, it does work in a confined environment like Node/V8. This has worked in most JS engines for a while now but had never been part of the ES spec before ES2015.
MongoDB
The underlying MongoDB driver createIndex function supports String, Array and Object index definitions. The parsing code is in a util function called parseIndexOptions.
If you specify an array of Strings, Array pairs, or Objects then the order will be fixed:
['location','type']
[['location', '2d'],['type', 1]]
[{location: '2d'},{type: 1}]
Also note that the createIndex code does use Object.keys to enumerate when it gets an Object of index properties.
Mongoose
The Schema#index function is documented as requiring an Object to pass to "MongoDB driver's createIndex() function" so looks to support passing through whatever options you provide.
There are a couple of places where indexes are further processed though. For example when you create a sub document with an index, the index needs to have the parent schema prefixed onto field names. On a quick glance I think this code still works with an Array but I can't see any tests for that in the Mongoose code so you might want to confirm it yourself.
Mongoose does have a compound index test that relies on Object property order.

Related

Sequelize: Force update for a JSON array

Sequelize won't update a JSON field under some circumstances.
For example, I have:
[[1]] (an array inside array)
And I'm trying to push something:
instance.arr[0].push(1); // [[1,1]]
instance.save();
// or:
instance.update({arr: instance.arr});
Now inside the instance I have changed the array and nothing changed inside the database. Not even a query is sent. :(
From the Sequelize website:
https://sequelize.org/master/manual/model-instances.html
The save method is optimized internally to only update fields that really
changed. This means that if you don't change anything and call save,
Sequelize will know that the save is superfluous and do nothing, i.e.,
no query will be generated (it will still return a Promise, but it
will resolve immediately).
That's good, but it seems like it doesn't work for JSON. Can I do a force update?
As of today, I have to do a deep copy of the array to save it.
I'm using MariaDB. I don't know if that matters.
It seems you have to specify that the field has changed
instance.changed( 'arr', true);
instance.save

A collection 'DBName.CollectionName' already exists MongoDB [duplicate]

I'm working on a node.js app that uses MongoDB and I read this from the docs:
db.collection
Fetch a specific collection (containing the actual collection information). If the application does not use strict mode you can can use it without a callback in the following way.
var collection = db.collection('mycollection');
First of all, what 'strict mode' is the doc referring to?
Also, is it a bad practice to grab the collection in this fashion? Without the callback, wouldn't I lose the ability to capture a potential connection error when trying to select the right collection?
db.collection('some_collection', function(err, collection) {
// query goes here
});
http://mongodb.github.io/node-mongodb-native/api-generated/db.html#collection
strict, (Boolean, default:false) returns an error if the collection
does not exist
Right there in the documentation.
That is there so your application may not create new collections itself and can only reference what has been created before. Hence the need for the callback, in order to trap the error.
It might be referring to Javascript's strict mode instead of a Mongo specific feature. strict mode enables some optional but backwards incompatible changes in the Javascript language that help catch some bugs:
What does "use strict" do in JavaScript, and what is the reasoning behind it?

Meteor Mongo BulkOp turning ObjectID into plain object

While using Meteor, I sometimes access the underlying Node Mongo driver so I can make bulk updates and inserts.
const bulk = Coll.rawCollection().initializeOrderedBulkOp();
bulk.insert({key_id: Mongo.Collection.ObjectID()}); // note key_id is an ObjectID
...
bulk.execute();
But the value of the key_id fields ends up being the plain subdocument {_str: '...'} when I look in the database after the insert.
Is there any way to use bulk operations in Node's Mongo library (whatever it is Meteor uses) and keep ObjectID's as Mongo's ObjectID type?
(There's many posts about the nature of the different ID types, and explaining Minimongo, etc. I'm interested specifically about the bulk operations converting ObjectID's into plain objects, and solving that issue.)
From Neil's top-level comment
On a native method you would actually need to grab the native implementation. You should be able to access from the loaded driver through MongoInternals [...]
Mongo.Collection.ObjectID is not a plain ObjectId representation, and is actually a complex object for Meteor internal use. Hence why the native methods don't know how to use the value.
So if you have some field which is an ObjectId, and you're using some method of a Meteor Collection's rawCollection (for example,
.distinct
.aggregate
.initializeOrderedBulkOp
.initializeUnorderedBulkOp
), you'll want to convert your ObjectId's using
const convertedID = new MongoInternals.NpmModule.ObjectID(
originalID._str
);
// then use in one of the arguments to your function or something
const query = {_id: convertedID};
before calling the method on them.

How to query models by a property that is an array

I'm trying to do a 'findOne' operation in a model that has an array property and filter the results to only list the item if the string im searching is in that array.
Example:
var AppUser = server.loopback.getModel('AppUser');
AppUser.create({
"name":"juan"
"favoriteLetters":["a","b","c"]
},function(){
AppUser.findOne({where:{favoriteLetters:'a'}},function(error,appUser){
console.log(error,appUser);
});
});
So in this case i want to find a 'appUser' that has a favorite letter 'a'.
Thanks.
As far as I understood, possibility of such kind of a query depends on the underlying datasource and is not supported yet for relational DBs. But should be fine with memory storage or mongodb. More details and syntax for query is here: https://groups.google.com/d/msg/loopbackjs/8c8kw8EMiPU/yev3lsmrTFUJ
For anyone else who lands here, that query in your model is correct (for Mongo anyways).
{where:{favoriteLetters:'a'}
Reference:
Find document with array that contains a specific value

How to obtain a MongoDb collection in NodeJS

There are two different methods to obtain a reference to a MongoDB collection - both of them are used throughout the official documentation.
There is
var mycollection = db.collection('mycollection)'
and there is
db.collection('mycollection', function(err, collection){
//use collection
}
I tend to use the second one because it is consistent with "db.createCollecion(collection, callback)"
What is the difference between these methods?
Is there any database interaction when using these methods?
If you look at the code for Database, currently around line 456, you'll see that the only difference between the two in the way you've used them is how the collection object is returned. If you specify a callback, then it's returned that way, otherwise, it's returned as the value to the function. If you set the options however and specifically the option strict to true, you need to use the callback. When strict is set to true, the collection is verified before continuing (asynchronously).
Given that collections can be created dynamically (and usually are upon first use), there often isn't need to use strict mode.
So, it's really matter of personal coding preference otherwise. There is normally no activity to the database when creating a Collection object via: db.collection('collectionname') with the exception I mentioned above.

Resources