Spring data and mongoDB - aggregation with java list - mongodb

I have the following document (#Document):
#Id
private String id;
private String fileName;
private String projectId;
private List<DocumentFileVersion> documentFileVersions;
private List<String> userIdBlackList; // here userIds are included
and this is my current aggregation:
final String userId = "5589929b887dc1fdb501cdbb";
final Aggregation aggregate = newAggregation(match(Criteria.where("projectId").in(projectId)),
group("fileName").count().as("amountOfDocumentFiles"));
final AggregationResults<DocumentFileAmount> groupDocumentFiles = mongoTemplate.aggregate(aggregate, DocumentFile.class,
DocumentFileAmount.class);
final List<DocumentFileAmount> documentFileAmounts = groupDocumentFiles.getMappedResults();
final int amountOfDocumentFiles = documentFileAmounts.size();
Now I will extend the aggreagation in that way that I only will have the DocumentFiles where userId (in this case "1234") is not in userIdBlackList.
Is there a possibility to do that, like in pseudocode:
final Aggregation aggregate = newAggregation(match(Criteria.where("projectId").in(projectId).and(userId).notInList("userIdBlackList")),
group("fileName").count().as("amountOfDocumentFiles"));
I would need something like this: ... .and(userId).notInList("userIdBlackList") ...
[EDIT]
I have tried this query:
final Aggregation aggregate = newAggregation(
match(Criteria.where("projectId").in(projectId).and(userId).and("‌​userIdBlackList").ne(userId)),
group("fileName").count().as("amountOfDocumentFiles"));
A Database entry can look like this:
{
"_id" : ObjectId("587e7cabafdaff28743f3034"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "Hydrangeas.jpg",
"projectId" : "587e7c95afdaff28743f302e",
"userIdBlackList" : [
"5589929b887dc1fdb501cdbb"
]
}
but .and(userId).and("‌​userIdBlackList").ne(userId) has no effect.
[EDIT2]
I have tried to simulate it in the mongo console too.
I have listed all documentfiles with the command db.DocumentFile.find().pretty():
db.DocumentFile.find().pretty()
{
"_id" : ObjectId("587f0d61473c92b933a68efa"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f0d61473c92b933a68ef9",
"active" : true,
"userIdBlackList" : [
"587f0d61473c92b933a68ef8"
]}
and my query looks like this:
db.DocumentFile.aggregate({ "$match" : { "projectId" : { "$in" : [ "587f0d61473c92b933a68ef9"]} , "‌​userIdBlackList" : { "$ne" : "587f0d61473c92b933a68ef8"}}}).pretty();
{
"_id" : ObjectId("587f0d61473c92b933a68efa"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f0d61473c92b933a68ef9",
"active" : true,
"userIdBlackList" : [
"587f0d61473c92b933a68ef8"
]}
I have expected that I do not get a documentfile because of this expression "‌​userIdBlackList" : { "$ne" : "587f0d61473c92b933a68ef8"}
Does anyone know what I am doing wrong?
[EDIT3]
I have this two documents and with the aggegate:
final Aggregation aggregate = newAggregation(
match(Criteria.where("projectId").in(projectId).and("‌​userIdBlackList").nin(userId)),
group("fileName").count().as("amountOfDocumentFiles"));
I get the amount of 2 but it should 1. I don't know what I am doing wrong?
db.DocumentFile.find().pretty()
{
"_id" : ObjectId("587f2228e232342f74b166f9"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f2228e232342f74b166f8",
"active" : true,
"userIdBlackList" : [
"587f2228e232342f74b166f7"
]}
{
"_id" : ObjectId("587f2228e232342f74b166fa"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile2",
"ending" : "jpg",
"projectId" : "587f2228e232342f74b166f8",
"active" : true,
"userIdBlackList" : [ ]
}

Have you tried using .nin
final Aggregation aggregate = newAggregation(
match(Criteria.where("projectId").in(projectId).and("‌​userIdBlackList").nin(userId)),
group("fileName").count().as("amountOfDocumentFiles"));

Related

MongoDB delete specific nested element in object array

I want to remove this
"lightControl" : 75
from this
{
"_id" : "dfdfwef-fdfd-fd94284o-aafg",
"name" : "Testing",
"serialNumber" : "fhidfhd8ghfd",
"description" : "",
"point" : {
"type" : "Point",
"coordinates" : [
10.875447277532754,
20.940549069378634
]
},
"ancestors" : [ ],
"metadata" : {
"measurement" : {
"high" : "40000.0",
"medium" : "25000.0"
},
"digitalTwin" : {
},
"emails" : [
""
],
"lastMeasurementDate" : "2010-03-04T11:32:06.691Z",
"lightControl" : 75
},
"tags" : [ ],
"createdAt" : ISODate("2019-12-07T15:22:10.988Z"),
"updatedAt" : ISODate("2020-03-08T15:38:21.736Z"),
"_class" : "com.test.demo.api.model.Device"
}
All I want is for this specific id, to completely delete the lightControl element from metadata. I have tried $pull, but I am probably missing something. Any ideas? Thank you in advance!
Your lightControl is not in array, for nested property, use the dot wrapped in doublequotes:
MongoDB shell:
db.getCollection('yourCollectionName').update({},{$unset:{
"metadata.lightControl":""
}})
In case you have a list of objects with _id(s) instead of updating directly, assume Node.js client for MongoDB is used:
// import mongodb from "mongodb"; // ES6
const mongodb = require("mongodb");
var client = MongoClient(...);
var coll = client["yourDbName"]["yourCollectionName"];
var objs = []; // <-- The array with _id(s)
for (let i=0; i<objs.length; i++){
let id = mongodb.ObjectID(objs[i]["_id"]);
coll.update({ _id:id },{$unset:{ "metadata.lightControl":"" }});
}

Add field to every document with existing data (move fields data to new field)

I have almost no experience in SQL or noSQL.
I need to update every document so that my fields "Log*" are under the new field "Log"
I found some help from this StackOverflow, but I am still wondering how to move the data.
Thank you very much
Original document
// collection: Services
{
"_id" : ObjectId("5ccb4f99f4953d4894acbe79"),
"Name" : "WebAPI",
"LogPath" : "Product\\APIService\\",
"LogTypeList" : [
{
"Name" : "ApiComCounter",
"FileName" : "ApiComCounter.log"
},
{
"Name" : "ApiService",
"FileName" : "ApiService.log"
}
]
}
Final Document
// collection: Services
{
"_id" : ObjectId("5ccb6fa2ae8f8a5d7037a5dd"),
"Name" : "InvoicingService",
"Log" : {
"LogPath" : "Product\\APIService\\",
"LogTypeList" : [
{
"Name" : "ApiComCounter",
"FileName" : "ApiComCounter.log"
},
{
"Name" : "ApiService",
"FileName" : "ApiService.log"
}
]
}
}
This requires MongoDB 4.2 or higher:
db.<collection>.updateMany({}, [
{$set: {"Log.LogPath": "$LogPath", "Log.LogTypeList": "$LogTypeList"}},
{$unset: ["LogPath", "LogTypeList"]}
])

Get only nested Object from MongoDB by Spring Data mongoDb or mongo template

{
"_id" : "a9582f59-f52b-4fc8-84ab-cdd0bfb8dead",
"_class" : "com.db.Category",
"name" : "Cricket",
"subCategories" : [
{
"name" : "Gloves",
"creationDate" : NumberLong("1527404341099"),
"modificationDate" : NumberLong("1527404341099")
},
{
"name" : "Stumps",
"creationDate" : NumberLong("1527404369882"),
"modificationDate" : NumberLong("1527404369882")
},
{
"name" : "Bat",
"brandList" : [
{
"name" : "MRF",
"productDetails" : [
{
"name" : "Bat 111",
"price" : "1224",
"imageUrlList" : [
"https://s3.us-east-1.amazonaws.com/gasports/1527792222680-Bat_111",
"https://s3.us-east-1.amazonaws.com/gasports/1527792228375-Bat_111"
]
}
]
}
],
"creationDate" : NumberLong("1527424021629"),
"modificationDate" : NumberLong("1527424021629")
}
],
"creationDate" : NumberLong("1527404340938"),
"modificationDate" : NumberLong("1527404340938")
}
This is Category Document. Category has subcategory,Subcategory has brands and Brand has products.
#Document(collection="productInfo")
public class Category extends BaseProductInfo<Category> {
#Id
private String uid;
private String name;
private List<SubCategory> subCategories;
//Getters ans setters..
Now I have to get only for specific subcategory or Brand or Product. For this currently I am doing iteration to get object.
Is there any way in Spring Data Mongo Repository to get nested Object ?
You can write a method like this in your Category Repository class
Category findBySubCategories_BrandList_ProductDetails_Name(String name)
Just pass the product name you want to fetch. spring-data-mongodb will formulate the query based on your meethod name and fetch the documents matching the query.

Mongodb put Documents array as the same level

I have this array of documents, I would like to put "table" on the same level like mastil_antenas and other variables. how Can I do that with aggregate?
I'm trying with the aggregate $project but I can't get the result.
Example of Data
[ {
"mastil_antena" : "1",
"nro_platf" : "1",
"antmarcmast" : "ANDREW",
"antmodelmast" : "HWXXX6516DSA3M",
"retmarcmast" : "Ericsson",
"retmodelmast" : "ATM200-A20",
"distmast" : "1.50",
"altncramast" : "41.30",
"ORIENTMAG" : "73.00",
"incelecmast" : "RET",
"incmecmast" : "1.00",
"Feedertypemast" : "Fibra Optica",
"longjumpmast" : "5.00",
"longfo" : "100",
"calibrecablefuerza" : "10 mm",
"longcablefuerza" : "65.00",
"modelorruantena" : "32B66A",
"tiltmecfoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114934746000.jpg",
"tiltmecfoto_fh" : "2017-10-18T05:51:22Z",
"az0foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115012727000.jpg",
"az0foto_fh" : "2017-10-18T05:55:21Z",
"azneg60foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115016199000.jpg",
"azneg60foto_fh" : "2017-10-18T05:55:36Z",
"azpos60foto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115020147000.jpg",
"azpos60foto_fh" : "2017-10-18T05:55:49Z",
"etiqantenafoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114920853000.jpg",
"etiqantenafoto_fh" : "2017-10-18T05:56:01Z",
"tiltelectfoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114914236000.jpg",
"tiltelectfoto_fh" : "2017-10-18T05:56:13Z",
"idcablefoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114900279000.jpg",
"idcablefoto_fh" : "2017-10-18T05:56:38Z",
"rrutmafoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114947279000.jpg",
"rrutmafoto_fh" : "2017-10-18T05:56:49Z",
"etiquetarrufoto" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114954648000.jpg",
"etiquetarrufoto_fh" : "2017-10-18T05:57:02Z",
"rrutmafoto1" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017114959738000.jpg",
"rrutmafoto1_fh" : "2017-10-18T05:57:12Z",
"etiquetarrufoto1" : "https://secure.appenate.com/Files/FormEntry/47929-92cdf219-3128-4903-8324-a81000602b9d171017115005545000.jpg",
"etiquetarrufoto1_fh" : "2017-10-18T05:57:27Z",
"botontorre4" : "sstelcel3",
"table" : { /* put all varibles one level up*/
"tecmast" : "LTE",
"frecmast" : "2100",
"secmast" : "1",
"untitled440" : "Salir"
},
"comentmast" : "",
"longfeedmast" : "",
"numtmasmast" : "",
"otra_marca_antena" : "",
"otro_modelo_antena" : ""
}]
Starting from MongoDB version 3.4 you could use $addFields to do this.
//replace products with what makes sense in your database
db.getCollection('products').aggregate(
[
{ //1 add the properties from subdocument table to documents
$addFields: {
"documents.tecmast" : "documents.0.table.tecmast",
"documents.frecmast" : "documents.0.table.frecmast",
"documents.secmast" : "documents.0.table.secmast",
"documents.untitled440" : "documents.0.table.untitled440"
}
},
{
//(optional) 2 remove the table property from the documents
$project: {"documents.table" : 0}
}
]
)
Step 1: use $addFields to grab properties from table inside documents.table and put them on documents
Step 2: (optional) remove property "table" from documents.
I hope this helps!!!

meteor client find is not working due to $eq

I subscribed to my servers's publication as follows:
Template.observedQuestions.onCreated(function(){
var self = this;
self.autorun(function(){
self.subscribe('observedQuestionsFeed');
});
});
Now I need to fetch my data using helper function:
Template.observedQuestions.helpers({
observedQuestionsList : function(){
questions = Questions.find({
observedByUsers : {$exists: true,$elemMatch:{$eq:Meteor.userId()}}});
return questions;
}
});
but it does not work due to $eq being not recognised in minimongo.
How to solve it?
doc sample:
{
"_id" : "rP4JP8jkprwwi3ZCp",
"qUserId" : "NLLW3RBXqnbSGuZ3n",
"type" : "question",
"date" : ISODate("2016-02-13T11:23:10.845Z"),
"subject" : "test",
"question" : "test",
"replies" : [
{
"rID" : "LphcqKnkTHf25SCwq",
"rUserID" : "NLLW3RBXqnbSGuZ3n",
"date" : ISODate("2016-02-13T11:23:10.847Z"),
"answer" : "reply1."
},
{
"rID" : "HxaohnEgxwNJLtf2z",
"rUserID" : "NLLW3RBXqnbSGuZ22",
"date" : ISODate("2016-02-13T11:23:10.848Z"),
"answer" : "reply2"
}
],
"observedByUsers" : [ "Bi24LGozvtihxFrNe" ]
}
Judging from your sample Questions document, the field observedByUsers is a simple array which contains user IDs.
As a result, you could simply use the following query:
Questions.find({observedByUsers: Meteor.userId()});