MongoDB Lookup MainField to FieldA or FieldB - mongodb

I need to join two MongoDB Colletions with lookup, the MainField to join from de first collection has to join with the other collection through Field A or Field B.
MainField is an array, with this structure [Doc1.FieldA, Doc2.FieldA, Doc3.FieldB,...].
FieldA is Unique-Index.
FieldB is Non-Unique-Index, it is for group FieldB with a unique value.
The problem is that I need to keep the order of the MainField Array.
I like to do something like this:
db.getCollection("collection1").aggregate([
$lookup: {
from: "collection2",
localField: "mainField",
foreignField: $or:["fieldA","FieldB"]
as: "mainFieldInfo"
}]
Is it possible to do this lookup or I need a different approach?
Collections examples, the documents are simplified there are more fields
in each document.
Collection Machines (1 example) :
{
"_id" : ObjectId("5c793a188021710636865c33"),
"MachineName" : "CER3A",
"NextJobs" : [ //--> MainField
"ST105862", // match with FIELD B - Flags.STS
"OFT083520", // match with FIELD A - Lote
"OFT083365",
"ST105946"
]
}
Collection Works (2 example, 1 to match with FieldA, 1 to match Field B):
Field A example:
FieldB*(Flags.STS)* is empty
{
"_id" : ObjectId("5c1b89d0b6e97d001816595e"),
"Lote" : "OFT083520", //--> FIELD A
"Flags" : {
"ShipsFinished" : true,
"PlanFinished" : true,
"Finished" : true,
"IdDefecto" : false,
"EstadoOF" : 4,
"GCT" : "GCT018929",
"PedidoVenta" : "",
"STS" : "", //--> FIELD B
}
}
Field B Example (2 docs):
FieldA*(Lote)* is diferent in each document, FieldB*(Flags.STS)* is equal
{
"_id" : ObjectId("5dcd78e2a2061070185400e2"),
"Lote" : "OFT083671", //--> FIELD A
"Flags" : {
"B2" : 1,
"EstadoOF" : 4,
"Finished" : false,
"GCT" : "GCT024270",
"LaSI" : 0,
"PedidoVenta" : "P056048",
"SPO" : "PO23579",
"STS" : "ST105862", //--> FIELD B
"Inks" : "true",
}
}
{
"_id" : ObjectId("5dcd78e2a2061070185401f0"),
"Lote" : "OFT083672", //--> FIELD A
"Flags" : {
"B2" : 1,
"EstadoOF" : 4,
"Finished" : false,
"STS" : "ST105862", //--> FIELD B
"ShipsFinished" : false,
"TipoOF" : 1,
"EstatIQC" : 1,
}
}

You have to use the other form of $lookup stage, which allow to perform multiple conditions for the lookup stage.
Here's the query you have to run :
db.machines.aggregate([
{
$lookup: {
from: "works",
let: {
"nj": "$NextJobs"
},
pipeline: [
{
$match: {
$expr: {
$or: [
{
$in: [
"$Lote",
"$$nj"
]
},
{
$in: [
"$Flags.STS",
"$$nj"
]
}
]
}
}
}
],
as: "linkedWorks"
}
}
])
You can test it here

Related

I am new to mongoDB need a query to delete the collections

I have two collections.
1.Equipment
db.getCollection("Equipment").find({
$and: [
{ $where: 'this._id.length <= 7' },
{ "model": "A505"}
]})
{
"_id" : "1234567",
"locationId" : "DATALOAD",
"model" : "A505",
"subscriberId" : "",
"status" : "Stock",
"headendNumber" : "4"
}
{
"_id" : "P13050I",
"locationId" : "1423110302801",
"model" : "A505",
"subscriberId" : "37",
"status" : "Stock",
"headendNumber" : "4"
}
I will get more than 100 documents (rows) Equipment collection.
2.Subscriber
db.getCollection('Subscriber').find({})
{
"_id" : "5622351",
"equipment" : [
"0018015094E6",
"1234567",
"ADFB70878422",
"M10610TCB052",
"MA1113FHQ151"
]
}
{
"_id" : "490001508063",
"equipment" : [
"17616644510288",
"P13050I",
"M91416EA4251",
"128552270280560"
]
}
In the Subscriber collection, I need to remove (get all the id from Equipment collection loop it) only the matches equipment field.
Forex from the above result, I need to remove only "1234567", and "P13050I"
Expected output.
db.getCollection('Subscriber').find({})
{
"_id" : "5622351",
"equipment" : [
"0018015094E6",
"ADFB70878422",
"M10610TCB052",
"MA1113FHQ151"
]
}
{
"_id" : "490001508063",
"equipment" : [
"17616644510288",
"M91416EA4251",
"128552270280560"
]
}
Please help me, anyone.
You can use the following to update records.
Let's find records which need to deleted and store them in array
var equipments = [];
db.getCollection("Equipment").find({ $and: [
{ $where: 'this._id.length <= 7' },
{ "model": "A505"}
]}).forEach(function(item) => {
equipments.push(item._id)
})
Now, iterate over records of the second collection and update if required.
db.getCollection('Subscriber').find({}).forEach(function(document) => {
var filtered = document.equiment.filter(id => equipments.indexOf(id) < 0);
if(filtered.length < document.equipment.length){
db.getCollection('Subscriber').update({"_id": document.id }, { $set: {'equipment': filtered}})
}
})
.filter(id => equipments.indexOf(id) < 0) will keep entries which is not present in initially populated array equipments and it will persist if there is any change.

Individual search result in multiple values in arrays

I have following model:
{
"_id" : ObjectId("5d61aaf8108e185191552bbb"),
"serials" : [
"e127av48-0697-4977-b096-5ce79c89a414",
"d163f80a-55ff-40fe-90b4-331ece5bebd5",
"4740021f-e9b5-4ca5-bf0e-8554c123bb94",
"320ffd42-f101-4b1d-8ff4-80bc693a29e6",
"fef5e68b-aed0-4a96-9488-7941c41d1c1f",
"2c0752ba-bf7a-4a3b-bd9f-14db4b2f8bae",
"6c5ff44d-5979-4bff-af12-9e6d282c3789",
"9c91bf91-72d7-4b71-827b-924947d6e93d",
"fb34b28e-afb1-4b6a-a3c1-5a1fe44246ee",
"91ab22ef-702f-4cbd-8919-a67a2b9a684c",
"ee1a7cb2-e088-47e6-a824-c8697df7d94c",
"0dc4c687-4db2-481e-a1a6-491320dede11",
"34612148-3e01-44ee-b262-de2035e63691",
"5ba85baf-e48a-40af-8578-55ff1a873c76",
"19fe3672-b6cb-4bb6-8d21-93412b938584",
"1d0d6f6d-1b49-461b-8661-ecbf43a6595e",
"d9a5455c-65ee-45e1-ae49-33cc15dec841",
"4a690a00-a76c-4d3e-aee3-78b2bb731b0c",
"ae331830-40b4-457c-8cc4-5d548f769c3e",
"fe3e460b-c89d-4ace-8a36-5ba2b53bf4d0",
"2cc6a2a0-e029-475f-a7fc-a46a79afb605",
"a7d07767-eada-4ce3-b083-9b048e9ae9f4"
],
"name" : "ApiCard",
"producer" : "Farmina",
"form" : "syrop",
"__v" : 0
}
I would like to retrive documents (multiple) from collection based on this serial numbers ("serials" field). For example i am finding:
[
"e127av48-0697-4977-b096-5ce79c89a414",
"d163f80a-55ff-40fe-90b4-331ece5bebd5",
"4740021f-e9b5-4ca5-bf0e-8554c123bb94",
"key that doesn't exist",
]
We have to assume that one of the serial number doesn't exist, so would like to get information for individual serial, expected output:
[
{
"serial":"e127av48-0697-4977-b096-5ce79c89a414",
"doc":{
....whole document where above serial is in array field "serials"
}
},
{
"serial":"e127av48-0697-4977-b096-5ce79c89a414",
"doc":{
....whole document where above serial is in array field "serials"
}
},
{
"serial":"e127av48-0697-4977-b096-5ce79c89a414",
"doc":{
....whole document where above serial is in array field "serials"
}
},
{
"serial":"key that doesn't exist",
"doc": null
}
]
I was trying the simplest solution - mongodb find by multiple array items, but unfortunately it'doesn't return info for individual serial number. I'am not sure it's possible to prepare this kind of query. I think some complex aggregation could perform it, but i don't even know this kind of pipelines.
Of course, i can get simple solution by using multiple aggregate or even find, but it could impact on performance, when application will be looking for 10000 records per request.
The following query can do the trick:
db.collection.aggregate([
{
$limit:1
},
{
$project:{
"_id":0,
"serialsToSearch":[
"e127av48-0697-4977-b096-5ce79c89a414",
"d163f80a-55ff-40fe-90b4-331ece5bebd5",
"4740021f-e9b5-4ca5-bf0e-8554c123bb94",
"key that doesn't exist",
]
}
},
{
$unwind:"$serialsToSearch"
},
{
$lookup:{
"from":"collection",
"let":{
"serial":"$serialsToSearch"
},
"pipeline":[
{
$match:{
$expr:{
$in:["$$serial","$serials"]
}
}
},
{
$project:{
"serials":0
}
}
],
"as":"searialsLookup"
}
},
{
$unwind:{
"path":"$searialsLookup",
"preserveNullAndEmptyArrays":true
}
},
{
$project:{
"serial":"$serialsToSearch",
"doc":{
$ifNull:["$searialsLookup",null]
}
}
}
]).pretty()
Data Set:
{
"_id" : ObjectId("5d61aaf8108e185191552bbb"),
"serials" : [
"e127av48-0697-4977-b096-5ce79c89a414",
"d163f80a-55ff-40fe-90b4-331ece5bebd5",
"4740021f-e9b5-4ca5-bf0e-8554c123bb94",
"320ffd42-f101-4b1d-8ff4-80bc693a29e6",
"fef5e68b-aed0-4a96-9488-7941c41d1c1f",
"2c0752ba-bf7a-4a3b-bd9f-14db4b2f8bae",
"6c5ff44d-5979-4bff-af12-9e6d282c3789",
"9c91bf91-72d7-4b71-827b-924947d6e93d",
"fb34b28e-afb1-4b6a-a3c1-5a1fe44246ee",
"91ab22ef-702f-4cbd-8919-a67a2b9a684c",
"ee1a7cb2-e088-47e6-a824-c8697df7d94c",
"0dc4c687-4db2-481e-a1a6-491320dede11",
"34612148-3e01-44ee-b262-de2035e63691",
"5ba85baf-e48a-40af-8578-55ff1a873c76",
"19fe3672-b6cb-4bb6-8d21-93412b938584",
"1d0d6f6d-1b49-461b-8661-ecbf43a6595e",
"d9a5455c-65ee-45e1-ae49-33cc15dec841",
"4a690a00-a76c-4d3e-aee3-78b2bb731b0c",
"ae331830-40b4-457c-8cc4-5d548f769c3e",
"fe3e460b-c89d-4ace-8a36-5ba2b53bf4d0",
"2cc6a2a0-e029-475f-a7fc-a46a79afb605",
"a7d07767-eada-4ce3-b083-9b048e9ae9f4"
],
"name" : "ApiCard",
"producer" : "Farmina",
"form" : "syrop",
"__v" : 0
}
Output:
{
"serial" : "e127av48-0697-4977-b096-5ce79c89a414",
"doc" : {
"_id" : ObjectId("5d61aaf8108e185191552bbb"),
"name" : "ApiCard",
"producer" : "Farmina",
"form" : "syrop",
"__v" : 0
}
}
{
"serial" : "d163f80a-55ff-40fe-90b4-331ece5bebd5",
"doc" : {
"_id" : ObjectId("5d61aaf8108e185191552bbb"),
"name" : "ApiCard",
"producer" : "Farmina",
"form" : "syrop",
"__v" : 0
}
}
{
"serial" : "4740021f-e9b5-4ca5-bf0e-8554c123bb94",
"doc" : {
"_id" : ObjectId("5d61aaf8108e185191552bbb"),
"name" : "ApiCard",
"producer" : "Farmina",
"form" : "syrop",
"__v" : 0
}
}
{ "serial" : "key that doesn't exist", "doc" : null }
Note: The query won't give expected output if the collection would be empty.
Aggregation stages details:
STAGE I: Limiting the records to 1, as initially, our motive is to inject the input array in aggregation. The injection would be done in no time.
STAGE II: Projecting the input array as serialsToSearch
STAGE III: Now we have the input array as a field, we can unwind it
STAGE IV: Lookup in the same collection with each field of the input array and check if the searched serial is present in serials array
STAGE V: unwinding the lookup output
STAGE VI: Projecting fields as per the response required.

Project matching field value from array

I want to change the document structure and only display the definition in the definitionarray that matches the idLanguage in the document. How do I do that?
Example of a document with an definition array of 3 elements (three different id of idLanguage):
{
"_id" : ObjectId("59bc29897d7934a6a7577ee0"),
"reference" : "FIIG=A23900 INC=62356",
"idTerm" : "0161-1#TM-218801#1",
"idLanguage" : "0161-1#LG-000002#1",
"statusTerm" : 0,
"idOrganisation" : "0161-1#OG-000194#1",
"idConcept" : "0161-1#01-000001#1",
"term" : "ZRYCHLOVAC ZçVERU, KE KULOMETU ",
"definition" : [
{
"_id" : ObjectId("59bc0bd77d7934a6a7243f05"),
"reference" : "FIIG=A23900 INC=62356",
"idDefinition" : "0161-1#DF-000001#1",
"idLanguage" : "0161-1#LG-000001#1",
"statusDefinition" : 0,
"idOrganisation" : "0161-1#OG-002462#1",
"definition" : "A metallic claw shaped pivoting item, designed to accelerate the weapon's recovery from recoil by assisting in realigning the breech with the barrel.",
"idConcept" : "0161-1#01-000001#1"
},
{
"_id" : ObjectId("59bc29047d7934a6a7370782"),
"reference" : "FIIG=A23900 INC=62356",
"idDefinition" : "0161-1#DF-283090#1",
"idLanguage" : "0161-1#LG-000002#1",
"statusDefinition" : 0,
"idOrganisation" : "0161-1#OG-000194#1",
"definition" : "Kovov‡ otocn‡ p‡kov‡ polo_ka pro zrychlov‡n’ obnoven’ stavu zbrane pred zpetn_m r‡zem /v_strelem t’m, _e napom‡h‡ osov_mu ztoto_nen’ z‡vorn’ku /z‡veru s hlavn’.",
"idConcept" : "0161-1#01-000001#1"
},
{
"_id" : ObjectId("59bc290b7d7934a6a73ce124"),
"reference" : "FIIG=A23900 INC=62356",
"idDefinition" : "0161-1#DF-668740#1",
"idLanguage" : "0161-1#LG-000005#1",
"statusDefinition" : 0,
"idOrganisation" : "0161-1#OG-000200#1",
"definition" : "Metalowy element wahliwy w ksztalcie szpona, przeznaczony do przyspieszenia powrotu broni po odrzucie poprzez wspomaganie ponownego ustawienia w linii zamka i lufy.",
"idConcept" : "0161-1#01-000001#1"
}
]
}
You can use $indexOfArray and $arrayElemAt to match the values. Earlier questions suggest you are using MongoDB 3.4 at least, so that should not be a problem:
db.collection.aggregate([
{ "$addFields": {
"definition": {
"$arrayElemAt": [
"$definition.definition",
{ "$indexOfArray": [
"$definition.idLanguage",
"$idLanguage"
}}
]
}
}}
])
That extracts from the array by "definition" ( the field ) at the matched position of idLanguage. So you would be replacing the "array" with the singular value that matches between those properties.
You may want this if understood your problem correctly
db.collection.aggregate([
{
$project: {
reference: 1,
defination: {
$filter: {
input: "$definition",
as: "elem",
cond: {$eq: ["$$elem.idLanguage", "0161-1#LG-000001#1"]}
// instead of "0161-1#LG-000001#1" you can use your variable
}
}
}
}
])
OR to return only definition.definition
db.collection.aggregate([
{"$unwind": "$definition"},
{"$match": {"definition.idLanguage": "0161-1#LG-000001#1"}},
{
$group: {
_id: "$_id",
defination: {$push: "$definition.definition"}
}
}
])

MongoDB Conditional validation on arrays and embedded documents

I have a number of documents in my database where I am applying document validation. All of these documents may have embedded documents. I can apply simple validation along the lines of SQL non NULL checks (these are essentially enforcing the primary key constraints) but what I would like to do is apply some sort of conditional validation to the optional arrays and embedded documents. By example, lets say I have a document that looks like this:
{
"date": <<insertion date>>,
"name" : <<the portfolio name>>,
"assets" : << amount of money we have to trade with>>
}
Clearly I can put validation on this document to ensure that date name and assets all exist at insertion time. Lets say, however, that I'm managing a stock portfolio and the document can have future updates to show an array of stocks like this:
{
"date" : <<insertion date>>,
"name" : <<the portfolio name>>,
"assets" : << amount of money we have to trade with>>
"portfolio" : [
{ "stockName" : "IBM",
"pricePaid" : 155.39,
"sharesHeld" : 100
},
{ "stockName" : "Microsoft",
"pricePaid" : 57.22,
"sharesHeld" : 250
}
]
}
Is it possible to to apply a conditional validation to this array of sub documents? It's valid for the portfolio to not be there but if it is each document in the array must contain the three fields "stockName", "pricePaid" and "sharesHeld".
MongoShell
db.createCollection("collectionname",
{
validator: {
$or: [
{
"portfolio": {
$exists: false
}
},
{
$and: [
{
"portfolio": {
$exists: true
}
},
{
"portfolio.stockName": {
$type: "string",
$exists: true
}
},
{
"portfolio.pricePaid": {
$type: "double",
$exists: true
}
},
{
"portfolio.sharesHeld": {
$type: "double",
$exists: true
}
}
]
}
]
}
})
With this above validation in place you can insert documents with or without portfolio.
After executing the validator in shell, then you can insert data of following
db.collectionname.insert({
"_id" : ObjectId("58061aac8812662c9ae1b479"),
"date" : ISODate("2016-10-18T12:50:52.372Z"),
"name" : "B",
"assets" : 200
})
db.collectionname.insert({
"_id" : ObjectId("58061ab48812662c9ae1b47a"),
"date" : ISODate("2016-10-18T12:51:00.747Z"),
"name" : "A",
"assets" : 100,
"portfolio" : [
{
"stockName" : "Microsoft",
"pricePaid" : 57.22,
"sharesHeld" : 250
}
]
})
If we try to insert a document like this
db.collectionname.insert({
"date" : new Date(),
"name" : "A",
"assets" : 100,
"portfolio" : [
{ "stockName" : "IBM",
"sharesHeld" : 100
}
]
})
then we will get the below error message
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
Using Mongoose
Yes it can be done, Based on your scenario you may need to initialize the parent and the child schema.
Shown below would be a sample of child(portfolio) schema in mongoose.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var portfolioSchema = new Schema({
"stockName" : { type : String, required : true },
"pricePaid" : { type : Number, required : true },
"sharesHeld" : { type : Number, required : true },
}
References:
http://mongoosejs.com/docs/guide.html
http://mongoosejs.com/docs/subdocs.html
Can I require an attribute to be set in a mongodb collection? (not null)
Hope it Helps!

mongodb findAndModify update element in array

There is an bson document:
{
"_id" : ObjectId("5718441f5116a60b08000b8c"),
"mails" : [
{
"id" : 2,
"a" : [
{
"a" : 1
},
{
"a" : 2
}
]
},
{
"id" : 1,
"a" : [
{
"a" : 1
},
{
"a" : 2
}
]
}
]
}
I need to return and clear the array "a" which belong to "mails.id == x" for given document. So I use findAndModify like:
db.mail.findAndModify({query: {"_id":ObjectId("5718441f5116a60b08000b8c")}, update: {$set:{"mails.$.a":[]}}, new: false, fields:{"mails":{$elemMatch:{"id":1}}}})
However this don't work. The problem is the $set should apply on one document in array rather than the whole document. So I need a projection to project it out.
If I left update to blank, it will return the desired part:
{
"_id" : ObjectId("5718441f5116a60b08000b8c"),
"mails" : [
{
"id" : 1,
"a" : [
{
"a" : 1
},
{
"a" : 2
}
]
}
]
}
But I don't know how to clear the array 'a' in 'mails'
You have to specify array element match in the query:
db.mail.findAndModify({query: {"_id":ObjectId("5718441f5116a60b08000b8c"), "mails":{$elemMatch:{"id":1}}}, update: {$set:{"mails.$.a":[]}}, new: false, fields:{"mails":{$elemMatch:{"id":1}}}})
mails.$ in you update matches the first matched element in the doc, so you have to match it in the query. Also, this query will update the doc, but it will return the old version, since you use new: false, if you want to get the updated version set it to true.