Given an array of strings and a collection with that string and primary key in MongoDB, how can I get the _id's of those strings if exists and insert a record if not present in the collection?
The collection has only name and its _id in it.
Related
I have 2 collections as the following:
Products
- name (String)
- price (number)
- category (ObjectID refs to Category collection)
Category
- name
And we have search query for example "some query"
I'm trying to get the records where product.name or product.category.name partially includes the search query
If you are using Mongoose populate, that uses DBRef, which is not easily used on the server side.
If the Products document contains only the _id of the corresponding Category document, you would need to use aggregation to read all Products documents, and for each one load all of the corresponding Category documents, then filter using a regular expression.
That is not scalable.
If you denormalize a little bit by having the Products document contain both the _id and name of the Category (assuming Category actually has other data), you could use a text index on the Products collection, or if you are using Atlas, full text search in the Products collection.
I am trying to do an aggregate join to find patient information in the 'patients' table based on 'patient_id' field in the bookings table. Aggregate join is not returning results even though there are matching records in the 'patients' table. Am I doing something wrong?
If the _id field in the "patients" collection is an ObjectId, it won't match when you compare to the string value stored in patient_id. You can use the $addFields aggregation step to add a new field to the documents with the "patient_id" field cast to an ObjectId, but I'd advise you to instead store reference id's as ObjectIds
I have inserted some json data into mongodb and I wanted to perform a simple search by matching only the values irrespective of the keys (Since keys are different for different documents) and wanted to return the id of the document. I don't know how to compare only by values in mongodb.
Example: Suppose if am searching for word "Knowledge" it should return all the ids of the document which contain the word "Knowledge" irrespective of its key value.
You need to use Wildcard Text Indexes.
db.collection.createIndex( { "$**": "text" } )
If there is a static superset of fieldnames, you may find text indexes and the $text query operator useful for word-based searches.
Create the text index on every potential field, and those contained in each document will be included.
From the MongoDB documentation they have stated six index types :
Single Field Index
Compound Index
Multikey index
Geospacial index
Text index
Hashed index
The documentation has also stated four index properties.
Unique Indexes
Partial indexes
Sparse Indexes
TTL Indexes
My questions are:
Can any index type have any index property?
Can an index type have more than one index property?
According to the docs: MongoDB creates a unique index on the _id field during the creation of
a collection. Does this mean when I search by Id MongoDB does not do a collection scan but instead uses the id index to execute the query efficiently? Or is the default id index just for uniqueness only? Does a unique index property always support faster queries?
I am using MongoDB via mongoose. When defining a schema in node.js does the field unique: true imply indexing of that will result to efficient search as opposed to a collection scan?
Can materialized views be indexed in MongoDB? If so how?
In the MongoDB documentation it states that MongoDB provides a number of different index types to support specific types of data and queries. Gut there is no explanation of what index properties are. How would you define index properties?
Can any index type have any index property?
Can an index type have more than one index property?
You can test yourself and find out.
Does this mean when I search by Id MongoDB does not do a collection scan but instead uses the id index to execute the query efficiently?
Yes.
Does a unique index property always support faster queries?
Uniqueness refers to a restriction on data which can be placed in the field which is indexed. Both unique and non-unique indexes allow fast retrieval of data queried by indexed fields.
Can materialized views be indexed in MongoDB?
If you are talking about https://docs.mongodb.com/manual/core/materialized-views/, "materialized views" in MongoDB are orthogonal to indexes. You can add indexes on what this page refers to as "output collection" (the argument to $merge) if you wish to query the "materialized view" efficiently.
MongoDB provides a number of different index types to support specific types of data and queries.
Geospatial index supports geo queries. Text index supports text search. Other indexes are general-purpose.
I am trying to insert data in mongoDB document but I am getting duplicate error in it.
While defining the schema, I have given the value of "role" as default:"customer".
But on adding data with different number, i am getting this error.
Following is the schema defined:
Where is the error?
Check if the collection have unique index on the column.
db.customers.getIndexes()
here you will get unique index on userName. drop it
It will return all the indexes. If it have unique: true then drop it and then insert records.
OR just use
db.customers.dropIndex('username_1')
It's not the error because of mobilenumber. it's because of username. you are trying to insert multiple username with null value which is violating unique rule.