Capped collections using MongoDB and SpringData - mongodb

I am trying to create a capped collection for logging using mongoTemplate. However my collection size is growing beyond the size I passed as arguments. Can anyone please help with this.
public synchronized MongoTemplate getTemplate() {
if (template == null) {
Mongo mongo = null;
mongo = new Mongo(addrs);
template = new MongoTemplate(mongo, this.dbName);
if(!template.collectionExists(HttpRequestEntity.class)){
CollectionOptions options = new CollectionOptions(4,4,true);
template.createCollection(HttpRequestEntity.class, options);
}
}
return template;
}
For saving I am calling save on this template instance
getTemplate().save(entity);

Got it working after I deleted the collection from mongo console. I guess it was use old meta data as template.collectionExists(HttpRequestEntity.class) was returning true.

Related

Complex mongodb query with Quarkus

I need to migrate a Spring Boot project to Quarkus. The project has been using Spring Data Mongodb for all the queries. Now I find it difficult to migrate complex queries.
One example is
public List<MyData> findInProgressData(MyConditions conditions) {
Query mongoQuery = new Query();
mongoQuery.addCriteria(Criteria.where("status").is(IN_PROGRESS));
mongoQuery.addCriteria(Criteria.where("location").is(conditions.getLocationCode()));
if (StringUtils.isNotBlank(conditions.getType())) {
mongoQuery.addCriteria(Criteria.where("type").is(conditions.getType()));
}
if (StringUtils.isNotBlank(conditions.getUserId())) {
mongoQuery.addCriteria(Criteria.where("userId").is(conditions.getUserId()));
}
mongoQuery.with(Sort.by(Sort.Direction.ASC, "createdAt"));
return mongoTemplate.find(mongoQuery, MyData.class);
}
How can I implement the conditional query with Quarkus?
You can probably try Panache with Mongodb.
You can pass Document into .find() method, In this object you can specify any criteria.
final Document document = new Document();
document.put("status","IN_PROGRESS");
...
result = MyData.find(document); // or MyDataRepository
But you'll need to adapt some of the code to Panache, which can be done either via extending PanacheEntity or PanacheEntityBase

How do you check if a collection is capped?

Before, using spring data mongo you could do something like mongoClient.getDB(db_name).getCollection(collection_name).isCapped(). But now the getDB is deprecated, you could still use it but there must be some other way to do it.
I tried doing mongoClient.getDatabase(db_name).getCollection(collection_name).some_function() but there is no similar function like isCapped() now.
You can achieve to find if a collection is capped by doing the following.
MongoTemplate mongoTemplate = new MongoTemplate(mongoClient(), getDatabaseName());
Document obj = new Document();
obj.append("collStats", "yourCollection");
Document result = mongoTemplate.executeCommand(obj);
result.getBoolean("capped")

ReferenceError: db is not defined while trying to find distinct entries in database

I am getting db is not defined when trying to use mongodb's distinct in meteor.
Template.displayinbox.helpers({
inboxlistings: function() {
itemscount = db.Messages.distinct( "fromUsername" ).count;
return db.Messages.distinct( "fromUsername" );
}
});
I want to be able to return only distinct documents in my collection from the username field and count all those documents that is posted by the fromUsername. How would I go about doing this in Meteor?
When you're querying anything in the Meteor code itself, you don't need to write db first. You have to use the variable name that used to instantiate the Mongo Object. Let's say you defined your Mongo db like this.
example = new Mongo.Collection('Messages');
then within your helper you just use the typical query using this object.
Template.displayinbox.helpers({
inboxlistings: function() {
var items = example.find();
return _uniq(items,function(i){return i.fromUserName;});
}
});

MongoDB udate query using class object in C#?

I am storing class object in mongodb like below,
try
{
Sample risk = new Sample();
risk.Name = "ABC";
risk.Enable = true;
risk.Sender = "IBM";
risk.Target = "CITI";
MongoServer server = MongoServer.Create("mongodb://localhost");
MongoDatabase db = server.GetDatabase("DATABASE");
db.GetCollection<StockQuote>("SMAPLETABLE").Insert(risk);
}
catch (Exception e)
{
MessageBox.Show("Error");
}
now i want to to update that same class instance like db.GetCollection<StockQuote>("SMAPLETABLE").Insert(risk); how can i do this one.
There is two approaches two update document:
1.Via Save method and update etire document
db.GetCollection<StockQuote>("SMAPLETABLE").Save(risk);
2.Via atomic update: update part of document. For example if you need update Enable field of document with Name "ABC":
db.GetCollection<StockQuote>("SMAPLETABLE").Update(
Query.EQ("Name", "ABC"),
Update.Set("Enable", false));
Some notes:
With atomic updates you can avoid concurrency issues. Atomic update it is like transaction within one document.

How do I get the date a MongoDB collection was created using MongoDB C# driver?

I need to iterate through all of the collections in my MongoDB database and get the time when each of the collections was created (I understand that I could get the timestamp of each object in the collection, but I would rather not go that route if a simpler/faster method exists).
This should give you an idea of what I'm trying to do:
MongoDatabase _database;
// code elided
var result = _database.GetAllCollectionNames().Select(collectionName =>
{
_database.GetCollection( collectionName ) //.{GetCreatedDate())
});
As far as I know, MongoDB doesn't keep track of collection creation dates. However, it's really easy to do this yourself. Add a simple method, something like this, and use it whenever you create a new collection:
public static void CreateCollectionWithMetadata(string collectionName)
{
var result = _db.CreateCollection(collectionName);
if (result.Ok)
{
var collectionMetadata = _db.GetCollection("collectionMetadata");
collectionMetadata.Insert(new { Id = collectionName, Created = DateTime.Now });
}
}
Then whenever you need the information just query the collectionMetadata collection. Or, if you want to use an extension method like in your example, do something like this:
public static DateTime GetCreatedDate(this MongoCollection collection)
{
var collectionMetadata = _db.GetCollection("collectionMetadata");
var metadata = collectionMetadata.FindOneById(collection.Name);
var created = metadata["Created"].AsDateTime;
return created;
}
The "creation date" is not part of the collection's metadata. A collection does not "know" when it was created. Some indexes have an ObjectId() which implies a timestamp, but this is not consistent and not reliable.
Therefore, I don't believe this can be done.
Like Mr. Gates VP say, there is no way using the metadata... but you can get the oldest document in the collection and get it from the _id.
Moreover, you can insert an "empty" document in the collection for that purpose without recurring to maintain another collection.
And it's very easy get the oldest document:
old = db.collection.find({}, {_id}).sort({_id: 1}).limit(1)
dat = old._id.getTimestamp()
By default, all collection has an index over _id field, making the find efficient.
(I using MongoDb 3.6)
Seems like it's some necroposting but anyway: I tried to find an answer and got it:
Checked it in Mongo shell, don't know how to use in C#:
// db.payload_metadata.find().limit(1)
ObjectId("60379be2bec7a3c17e6b662b").getTimestamp()
ISODate("2021-02-25T12:45:22Z")