child referencing in an array VS searching through the children collection - mongodb

I was wondering if there is any performance difference between having to populate records in a document which are stored as:
createdShapes[
ObjectId('AAA'),
ObjectId('AAA'),
ObjectId('AAA'),
ObjectId('AAA')
]
and between searching through the whole collection as:
CreatedShapes.findMany({createdBy: user._id})
why would I need to store Ids in an array if I can just search using findMany()?

Related

Is there any possible do upsert functionality in "array of an object" using firestore query?

Example :[{
inst:"EVA",
std:"12th"
},
{
inst:"KSF",
std:"12th"
}]
As per the above example, In my case if "inst: "EVA" is already there in the "qualification" array so we need to update the object from the existing one.
Then "inst: "KSF" does not already exist in the "qualification" array so we need to add that one.
Help me if there is any way to upsert using firestore query.
There is no "upsert" operation for objects in arrays. If you need to make changes to that array, you will have to read the document, modify the contents of the array in memory, then update the document with the new contents of the array.
Arrays of objects usually do not work the way that people want, given their limitations on querying and updating. It's usually better to store data as documents in a nested subcollection, so they can be more easily queried and updated by the contents of their fields.

How to use DocumentReference in firestore query

I have a Firestore Database with two collections, one is called "shops" and the other is called "groups".
In the "shops" collection, there are some documents that represent different shops in a city and in the "groups" collection there is a document with a single field that is an array of reference.
In the array, I saved the last 20 shops that were inserted in the "shops" collection.
I don't know how to get the documents by the reference save in the array.
My method needs to return a Stream<List<Shop>>, I need an idea to resolve my problem, I don't want to use a field in shops' document to save the time when it was inserted.

Unable to remove object id from a sub-document made up from array of object ids

I have two collections "notes" and "users". In users collection, I have stored array of object ids of notes that are created by a specific user. I just want to figure out a way such that, when a document from "notes" collection is removed, at the same time the object id of that particular note stored in "users" collection will also get removed.
I am using express and mongoose. I have attached images of these two collections.Notes collection
Users collection

pymongo: searching and editing fields within document

Say i have this database "list", which contains a collection called "users", which contain, among others, an object called "david"
{u'_id': u'david', u'url': u'url3', u'old_url': u'url3', u'wishlist': [[u'Jenara', u'shards', u'nm'], [u'force of will', u'mm2', u'nm'], [u'pact of negation', u'mm', u'nm'], [u'all is dust', u'mm4', u'nm']]}
how can i use pymongo to edit the arrays within the wishlist field? say i want to remove one of the four arrays, or edit one of them?
In order to update an element in an array, use $set. Here is an example - updating the second element and setting it's value to ["something", "else"]:
db.users.update({'_id': 'david'}, {"$set": {"wishlist.1": ["something", "else"]}})
As for the removing an item from an array by index, it's not that easy and straightforward, see:
In mongoDb, how do you remove an array element by its index
How to delete n-th element of array in mongodb

How does mongodb index lists

For example: If I had a db collection called Stores, and each store document has a list of the items they sell, and stores generally share items, then how would mongodb build an index on that?
Would it build a btree index on all possible items and then on each leaf of that tree (each item) will reference the documents which contain it?
Background:
I'm trying to perform queries like this using an index:
db.store.find({merchandise:{$exists:true}}) // where 'merchandise' is a list
db.store.find()[merchandise].count()
would an index on 'merchandise' help me?
If not, is my only option creating a separate meta field on 'merchandise' size, and index that?
Schema:
{ _id: 123456,
name: Macys
merchandise: [ 248651234564, 54862101248, 12450184, 1256001456 ]
}
From your document sample if you build your index on merchandise it will be multikey index and that index will be on every item on the array. See Multikey Indexes section in here.
If merchandise is an array of subdocuments, indexing over merchandise will put the index on all field of subdocument in the array. With index you can make queries like
db.store.find("merchandise":248651234564) and it will retrieve all document having merchandise 248651234564
For getting count of merchandise, you can get only get the size of merchandise field of one document like db.store.find()[index].merchandise.length. So creating a seperate field on merchandise size and indexing is a feasible option, if you want to run queries based on merchandise size.
Hope this helps
If you index a field that contains an array, MongoDB indexes each value in the array separately, in a multikey index. When you have 4 documents inside an array, each will act as a key in the index and point to the mentioned document(s).
You can use multikey indexes to index fields within objects embedded in arrays. That means, in your array, you can index a specific field in each document. For example: stuffs.thing : 1.
Read more about Multikey Indexes
Whether you need these indexes would depend on:
How many queries rely on that specific field?
How many updates, inserts hit that specific field (array)?
How many items will that array contain?
...
Remember that indexes slow writes as they need to be updated as well. I'd consider an explain on my queries to measure performance.