How to perform generic lookup in mongoDB? - 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.

Related

Mongo db - Querying documents using nested field (nested array and objects)

{
"_id":"12345",
"model":{
"T":0,
"serviceTask":[
{
"_id":"6789",
"obj":{
"params" :{
"action": "getEmployeeData",
"employeeId":"123"
},
"url":"www.test.com",
"var":"test",
},
"opp":100
}
]
}
}
I have similar structured documents in my collection. How do I query the documents that match action value to "getEmployeeData". I tried dot notation with $elemMatchbut couldn't get the results.
Dot notation works fine here, it will return the document if serviceTask contains at least one action set to getEmployeeData
db.collection.find({
"model.serviceTask.obj.params.action": "getEmployeeData"
})

MongoDB new ObjectId over array giving me same Id for all

I am trying to update items in an array with unique ObjectIds (meaning add an object ID to array object that are missing them)
If I have an array of shirt objects in my collection and I try this:
db.people.update({
$and : [
_id: ObjectId('5eeb44c6a042791d28a8641f'),
{
$or: [
{ 'shirts._id': { $eq:null } },
{ 'shirts._id':{ $exists:false } }
]
}
]
},{
$set: { 'shirts.$[]._id': new ObjectId() }
},{
"multi" : true
}
);
It generates IDENTICAL ObjectsIDs for each array element, I would put an unique index on this however, the use case probably wont see more then 2-3 items in the array with edge cases hitting 5-6, which seems like an abuse of an index
How can I update multiple records or multiple array objects with a unique ObjectId?
When you use $set you're telling mongo to set that value to all matching elements. If the elements in the array are already defined as schemas, mongo will issue new ObjectIds for each one of them automatically.
Alternatively, you can use forEach and iterate over each matching element creating a new ObjectId.

In MongoDB, how do I update by matching an element within an array of an Embedded query?

Say you have an document saved like this:
{
_id: "1",
thingy: {
moreStuff: [
{
_id:"a",
moreComplicated: [
{
_id:"a1",
info: "test"
},
... // More similar elements
]
},
... // More similar elements
]
}
}
Suppose you already have a way to get the query of the document:
db.getCollection("thingies").find({"_id": 1});
Now you're looking for a way to update the "info" section inside the "a1" object nested within all those other objects and arrays. The only catch to this is that "moreStuff" and "moreComplicated" arrays are being updated constantly, so aiming by array location (like thingy.morestuff.0.moreComplicated.0.info) won't work. The only way to do it is by looking up the unique mongoID of each object.
How would you query the arrays by matching an element to value instead of an index? Bonus points if you can have a "find" query return a similar object.

Retrieve array of field's values from MongoDB collection

I have an abccollection collection whose documents look like:
{
id:123
focusPoint:89652.33
}
I want to retrieve all focusPoint values so that output looks like:
[89652.33, 89999.223, 99666.45, ...]
If you want to get a list without duplicates:
db.abccollection.distinct("focusPoint")
Otherwise, keeping duplicates:
db.abccollection.find({}, { _id: 0, focusPoint: 1 }).map((doc) => doc.focusPoint)
With that you will retrieve all the documents in the abccollection collection and project just the focusPoint field. The raw output of that query (before the map) will be an array of documents with a single field:
[{ focusPoint: 89652.33 }, { focusPoint: 89999.223 }, ...]
If you don't want MongoDB to do the .map in MongoDB, you can also do it in your application.

Create unique indexes for document's objects stored in an array

How do one create unique indexes for document's objects stored in array?
{
_id: 'documentId',
books: [
{
unique_id: 1,
title: 'Asd',
},
{
unique_id: 2,
title: 'Wsad',
}
...
]
}
One thing I can think of is autoincrementing. Or is there any mongo way to do so?
if you remove the _id field from your doc, mongo will automatically add one for you, which is:
guaranteed to be unique
contains the timestamp of creation
lots of other features.
see here: https://docs.mongodb.com/v3.2/reference/method/ObjectId/
Looking at the example object again, are you referring to the ids in the books array?
If so, you can assign them with ObjectIds as well, just like in the document root's _id field:
doc.books.forEach(x => { x.unique_id = new ObjectId() } );