I was just reviewing some code, and saw such property in the mongoose scheme:
names: {
type: [String],
index: true
}
As far as I understand how indexes work, they are binary trees, and how is this going to be organized as a node of a tree? Is there at all any sense of indexing such property?
'If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array.' Per MongoDB documentation on multikey index.
Related
I have a Post model that has a publisher property defined in its schema (I'm using Mongoose). The publisher property is a string that refers to a publisher's name.
I also have an array called sourceNames that holds all the different publisher names. I want to query my database for ALL the posts whose publisher matches any one of the array elements in sourceName. My current query looks like this:
const query = postModel
.find({ publisher: { $all: sourceNames } })
.limit(limit)
.skip(startIndex);
My query isn't returning anything when I exec it. Does anyone know if what I'm trying to do is possible in a single query (Rather than loop over sourceNames and make a query for each individual element?
Short
Just replace $all with $in
Expl
$all is trying to match an array with all elements in your array.
$in instead, tries to match a string or array with one in the array.
I want to create partial index for the collection, but the index should be applied to documents by conditions. For example, I want to check uniqueness of documents only if they have the certain field value. In other words, I'm looking for some construction of index creating:
db.person.createIndex(
{ age: 1},
{ partialFilterExpression: { age: { $gte: 18 }}
);
This example is from MongoDB and it is applying index on documents with field 'age' value greater then 18
There is no way to create a "filtered index" (like you can in SQL). According to the docs, you can include attributes, but not conditionally.
You could try a sparse index, but I think your best bet is adding the age attribute to a "skiplist" index, which supports sorting and gt/lt evaluation.
Make sure you use the explain feature to validate index usage.
I am using MEAN Stack with Node, Express and MongoDB (mongoose ODM) in the backend.
Now, I have a schema model for a collection that has an _id value that is overwritten and that should be represented by a long or another big number datatype with at least 18 positions. I wanted to use mongoose-long for that.
The reason for that is, that I have some kind of "compound key" from 2 collections, which I represent by 12 positions from one collection, and then increment the other 6 number positions from the other collection which I increment by 1. This way, I can associate which of the collections belong together by the number prefix.
However, when I generate some data (float) and enter a long number for _id like "123456789123456778" and pass it to the mongodb, it gets saved, but there seems to be a rounding to the last three positions (hundreds) of the number when I query for the id field.
Also, if I increment the number by 1, it cannot save the entry because of duplicate keys. Is this a mongoose / MongoDB problem, or is it rather a javascript or python client side problem? I have read here: Mongodb can't find object with too long _id, that you can't query too long id numbers, maybe it is the same for entering too long numbers...
name: 'MongoError',
message: 'E11000 duplicate key error collection: dnz.fps index: _id_ dup key: { : 1.234567891234568e+17 }',
driver: true,
code: 11000,
index: 0,
errmsg: 'E11000 duplicate key error collection: dnz.fps index: _id_ dup key: { : 1.234567891234568e+17 }',
getOperation: [Function],
toJSON: [Function],
toString: [Function] }
Is there some workaround for this? Or for getting the big number sizes right in MongoDB?
I also thought about changing my data model, and using like 2 id's for my Collection, but which would change all my query logic again, and I don't know if this gets messy when I create new instances of my model. e.g. I would have to pass the id value from the created instance and insert it into the other collection in some way...
Has anybody a better idea how to work around this big number problem?
I am writing a mongoose schema, and I would like to understand the properties of the same.
Here is my schema:
var UserSchema = new Schema({
name: String,
username: { type: String, required: true, index: { unique: true }},
password: { type: String, required: true, select: false }
});
Why required is not declared for `name' - ?
Why required declared?
What is select - true/false -means?
When the index - should declared any why?
Why required is not declared for `name' - ?
Answer: When a field is mandatory to fill then in that case we mention it as required. So here "name" is not required or mandatory field.
Why `required' declared?
Answer: As mentioned above, When a field is mandatory to be filled then in that case we mention it as required.
What is select - true/false -means?
Answer: This means that it will not be returned by default in the data when you fetch the document. you can specify if this path should be included or excluded from query results by default.
Schema options
When the index - should declared any why?
Answer: Index should be declared when you are searching data on that field frequently so when you create indexing on that field in that case it do not search that field in all the collections it will search value for that field using index and will return result very quickly.
How indexes work in mongodb
Here, these act as model for your project. So, required is used as validation and index is working as index over that field
Now you have two ways :
either put validation over here in schemas/models
or just manually create validation for form at frontend using JS/Jquery and then long route
Now your answers:
Name is not compulsory to be filled in. That's why no required is put over there.
when there is mandatory to fill any value for that field. Then required is used in schemas.
True/False enables or disables the usage of validation over that field. If you are using false means filling in for that field isn't compulsion at all. But using false is considered a good practice.
Index is special data structure which are used for increasing performance during read/search operations. It increases the speed of operations and are stored in memory.
whenever we have to validate the particular field, so we used required.
required: true means you must fill that field.
required: false means you may or may not fill that field, but its a good practice.
Given a CouchDB view that emits keys of the following format:
[ "part1", { "property": "part2" } ]
How can you find all documents with a given value for part1?
If part2 was a simple string rather than an object startkey=["part1"]&endkey=["part1",{}] would work. The CouchDB docs state the following:
The query startkey=["foo"]&endkey=["foo",{}] will match most array keys with "foo" in the first element, such as ["foo","bar"] and ["foo",["bar","baz"]]. However it will not match ["foo",{"an":"object"}]
Unfortunately, the documentation doesn't offer any suggestion on how to deal with such keys.
The second element of your endkey value needs to be an object that collates after any possible value of the second element of your key. Objects are compared by property-by-property (for example, {"a":1} < {"a":2} < {"b":1}) so the best way to do this is to set the first property name in your endkey to a very large value:
startkey=["part1"]&endkey=["part1", { "\uFFF0": false }]
The property name of \uFFF0 should collate after any other property names in the second key element, and even works when the second element is an empty object or has more than one property.