Indexing of Object keys in MongoDB - mongodb

I have this structure
{
"_id": "willwill",
"rental": {
"hitchhikergalaxy": {[...]},
"animalfarm": {[..]}
}
}
Which [..] is a embedded document storing the details of the rental (and the exactly same data also exists on another collection) and the rental Object's keys are _id of the book collection.
How do I ensureIndex() this query?
db.users.find({"rental.animalfarm": {'$exists': true}})
(I use MongoDB on PHP, but the example above is in JavaScript)

You might want to think about refactoring the document such that:
"rental":
[
{ "name":"hitchikergalaxy", "attributes": { your stuff } },
{ "name":"animalfarm", "attributes": { your stuff } }
]
Now you have a path to ensureIndex "rental.name"
You can further find all documents where any user rented animalfarm using:
db.users.find( { "rental.name": "animalfarm" } )
I've been down the path of using data as keys/names -- and it always seems to make things more complicated than they need to be.

Related

Mongo adding a field to a specific object in an array of objects using updateOne during a bulkUpdateOps

Use case
Adding a field to a specific object in an array of objects using updateOne during a bulkUpdateOps
Blockers
I have been unable to find a way to identify and update a specific object in the subdocument array of a specific record.
I only have access to this DB through MongoDB Compass, and I plan to use the provided mongosh tool.
Data example
Our purchaseorders model looks like this:
{
_id: uuid,
...rest,
documents:[
{
_id:uuid,
forignKey2:string (optional),
keyIWantToAdd:string (optional),
...rest
}
]
}
So if I have
{
_id:*1,
documents:[
{
_id:*2,
forignKey2:'no-test',
...rest
},
{
_id:*3,
forignKey2:'test',
...rest
},
]
}
I want to add a key and value like this (really I'm willing to do anything to set these values, this is just the closest I have been able to get):
var bulkUpdateOps = db.purchaseorders.initializeOrderedBulkOp();
bulkUpdateOps.find('*1').updateOne({
$set:{
documents.[index of document object with forignKey2:'test' or _id:*3 whichever is easier].keyIWantToAdd:'valueIWantToAdd'
}
})
bulkUpdateOps.execute();
Any help or suggestions would be greatly appreciated.
#rickhg12hs posted exactly what I was looking for. For anyone else using mongodb.com/docs/manual/reference/method/Bulk.find.arrayFilters the bulk find is being ran on an array of objects like this: { grades:[ { grade: 85, mean: number } ] }

Doing a difficult mongoDB find

I need to find a document by an ID of a user which is in an array in that document, which looks like
{
"uuid": "000-000-000whatever",
users:
[
{
"id":"id1",
"role":"role1"
},
{
"id":"id2",
"role":"role1"
}
]
}
and while I know that for that i could in mongo do "users.id" as a filter, i don't have a clue on how to do it using bson from Go.
The smartest thing I could figure was something along the line of
bson.M{"users": bson.A{bson.M{"id":id}}}
but needless to say, it didn't work.
It's the "same" in Go too:
bson.M{"users.id": id}

How to perform generic lookup in mongoDB?

Suppose I have this collection:
{
"_id":{
"$oid":"5fc2950883c843a134980620"
},
"name":"Legs",
"url":"https://www.youtube.com/watch?v=yXH-_hMf_Vs&t=527s",
"tags":[
{
"$oid":"5fc82b4969562b1584bed619"
},
{
"$oid":"5fcbe6846674f842d41c6bf1"
}
],
"exercises":[
{
"$oid":"5fc29042d8afdc3aec64e8f1"
},
{
"$oid":"5fce7025371a883f08e51239"
}
],
"date":{
"$date":{
"$numberLong":"1602277200000"
}
}
}
I want my query to return to the full data, which means that in each time I encounter object id I want to go to that collection and get the whole object. For example when I am in the tags array I want for each tag id to be replaced with the whole object from the tags collection.
How can that be achieved with a mongo query? I don't want to store all the objects in the same collection, i.e. the tags ids for example are foreign keys in this collection and primary keys in the tags collection.
Exactly the same should be applied for each array that contains ids, like exercises in this example.

update array of array object elements in mongoose

I have schema data like below
{
id:'5d60fd38f6999a7792c940a4'
name:'test',
department:[
{
d_name:'Rd',
_id:'5d61092b1f234c11348eb831'
equipements:[
{
id:'5d637abd7ddd183263fa4ebc'
e_name:'first'
},
]
}
]
}
and I'm try to update the equipments by following way
College.updateOne(
{
_id: productObjectID,
'department._id': variantObjectId
},
{ $set: data }
);
but unfortunately this query is not update my equipment data .
can let me know what is right way to update. thanks
You need to search with the equipements.id in order to udpate the name
This would work
College.updateOne({
_id: productObjectID,
"department._id": variantObjectId,
"department.equipements.id": equipementId
},{ "$set": {"department.equipements.$.name": "updatedName"} });
I assume you're trying to achieve the below:
db.dummyTest.updateOne({ "_id" : ObjectId("5d63a1791b761bfc0420e590"),
"department._id": "5d61092b1f234c11348eb831",
"department.equipements._id": "5d637abd7ddd183263fa4ebc" },
{ $set: { "department.$.equipements.0.e_name.0": "Update to last" }})
MongoDB's support for updating nested arrays is poor. So you're best off avoiding their use if you need to update the data frequently
Option 1: Make department its own collection (make department, as sub document instead of array)
Option 2: Achieve it through program

How to push list of object ids from another query into an array attribute of a document in MongoDB?

What I need is something like:
db.categories.update(
{ _id: ObjectId("52824e116a4dec0000000004") },
{ $push: { scores: { $each : db.products.find({"category": ObjectId("51cedfb29b33fc0800000015")})} }
)
Do I need ORM tool for this? Or can I do with shell?
You probably just want to use JavaScript in the shell. It might look something like this:
// not sure what your schema looks like, but you'll want your find to
// get you the right documents and format
var ids = db.products.find({"category": ObjectId("51cedfb29b33fc0800000015")});
db.categories.update(
{ _id: ObjectId("52824e116a4dec0000000004") },
{ $pushAll: { scores: ids }
}
);
You'll have to make sure that ids is an array of object ids. The current find you have looks like it will return the entire documents. If you need more guidance, I'd suggest adding your schema to your question as well.