Doing a difficult mongoDB find - mongodb

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}

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 } ] }

Update with same document field in mongodb

I have a this document.Which has a schema of below:
rental:{
total:Number,
due:Number
}
For example let us assume the document is filled with values like this:
rental:{
total:350,
due:10
}
I want to replace the value of 'total' to 'due'.So i want it to be like this:
rental:{
total:350,
due:350
}
I came accross $set,i did something like this:
PS:"User" is the name of the model.(which i havent refrenced here)
User.updateMany({},{$set:{'due':"$total"}},function(err,..}{
//do whatever
}
But this didnt work out.I ran into a CastError.
I also came accross '$replaceWith'.But i didnt understand a bit on how to use that in my case.Any help is appriciated.Thank you
You can use below query
db.collection.update(
{ },
[{ "$set": { "due": "$total" }}]
)

get the particular field value from mongodb

In mongodb i saved document like this.
"Angela_Merkel": {
"birthPlace": "Hamburg,West_Germany",
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Angela-Merkel-2014.jpg?width=300",
"almaMater": "Leipzig_University",
"birthDate": "1954-07-17",
"spouse": "Joachim_Sauer"
}
There are many person's information like this way. Now if I want to get all the information of "Angela_Merkel" or only a particular like "birthdDate" of "Angela_Merkel" then what will be the query?
Like chridam says would be more practical that you refactor your documents like this:
{"name": "Angela_Merkel",
"birthPlace": "Hamburg,West_Germany",
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Angela-Merkel-2014.jpg?width=300",
"almaMater": "Leipzig_University",
"birthDate": "1954-07-17",
"spouse": "Joachim_Sauer"
}
Inside the "people" collection (Its a convention name the collections as plurals, beeing a set of documents)
Then you could do:
db.people.find({"name":"Angela Merkel"},{"_id":0," "birthdDate":1 })
To get:
{"birthDate": "1954-07-17"}
Anyway you can get with your document what you want this way:
"birthDate":
db.person.find({"Angela_Merkel.birthDate": "1954-07-17"})
or all the info:
db.person.find({"Angela_Merkel": { $exists: true}})
But doesn't have much sense do it this way.
PD: Edited to include last part.

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.

Indexing of Object keys in 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.