MongoDB unique index allows duplication - mongodb

I am new in mongoDb and I have a problem with mongock. Previously I had an index defined by annotation like below. In my unit test I have a case which tries to save same value on the same field and it throws exception as expected.
#Indexed(unique = true)
private String name;
I removed #Indexed annotation and I created index by mongock migration. But in this time I am able to insert a record with same value for this column without getting exception. So what is wrong here and why it is not working as it works above. I created my migration as :
collection.createIndex(
new Document(new ImmutableMap.Builder<String, Object>()
.put("name", 1)
.build()),
new IndexOptions().name("name_idx").unique(true),
);

Without providing information about the version you are using, it's hard to say.
However I believe your issue can be related to an old existing bug.
Please use the last version 4.0.8.alpha, which is alpha as features are still being added, but very stable(there is no official documentation yet, but you can see the sample project here) or the last official stable version: 3.3.2.
Please notice that in both case you should use MongockTemplate, instead of MongoTemplate.
I believe that will solve your issue. Otherwise, please provide version and stack trace.

Related

CRM D365 update polymorphic/multitable lookup via Plugin

I'm trying to update a polymorphic lookup column on a table via plugin code and it doesn't seem happening. Wondering what additional I’m missing.
var entity1Id = "af435c64-a264-4f2a-9cee-22069ce36b3c";
var destinationEntity = new Entity("bookableresourcebooking", new Guid(myBookingGuid));
destinationEntity["my_ploymorphiclookupid"] = new EntityReference("my_entity1", new Guid(entity1Id));
organizationService.Update(destinationEntity);
my_ploymorphiclookupid is a polymorphic lookup (N:1) to my_entity1
Code looks fine. If you didn’t see any runtime errors in plug-in trace log, then few diagnosis steps needed.
Check audit history of that record
Chances of any other plugin steps replacing the value back or rolling back
Try to profile/debug the plugin for clues

Cannot set UDTs in a jooq insert, consistently getting a StackOverflowError

The SQL is pretty simple:
CREATE TYPE audit_fields AS (
creation_time_ms BIGINT
);
CREATE TABLE users(
...
audit AUDIT_FIELDS NOT NULL
);
These are two ways I have tried to do this:
UsersRecord getRecord() {
return new UsersRecord()
.setSomeField(...)
.setOtherField(...)
.setAudit(new AuditFieldsRecord(System.currentTimeMillis()));
}
create.insertInto(Tables.USERS)
.set(getRecord())
.execute();
And, this:
create.insertInto(Tables.USERS)
.set(getRecord())
.set(Tables.USERS.AUDIT, new AuditFieldsRecord(System.currentTimemillis()))
.execute();
I am simply getting a StackOverflowError, with the following block repeating throughout (obviously an infinite-recursion):
at org.jooq.impl.UDTConstant.<init>(UDTConstant.java:62)
at org.jooq.impl.DSL.val(DSL.java:12781)
at org.jooq.impl.DSL.val(DSL.java:12732)
at org.jooq.impl.DSL.inline(DSL.java:12590)
at org.jooq.impl.UDTRecordImpl.toString(UDTRecordImpl.java:141)
at java.lang.String.valueOf(String.java:2994)
at org.jooq.impl.AbstractParam.name(AbstractParam.java:104)
at org.jooq.impl.AbstractParam.<init>(AbstractParam.java:78)
at org.jooq.impl.AbstractParam.<init>(AbstractParam.java:74)
at org.jooq.impl.UDTConstant.<init>(UDTConstant.java:62)
at org.jooq.impl.DSL.val(DSL.java:12781)
at org.jooq.impl.DSL.val(DSL.java:12732)
at org.jooq.impl.DSL.inline(DSL.java:12590)
at org.jooq.impl.UDTRecordImpl.toString(UDTRecordImpl.java:141)
at java.lang.String.valueOf(String.java:2994)
at org.jooq.impl.AbstractParam.name(AbstractParam.java:104)
at org.jooq.impl.AbstractParam.<init>(AbstractParam.java:78)
at org.jooq.impl.AbstractParam.<init>(AbstractParam.java:74)
at org.jooq.impl.UDTConstant.<init>(UDTConstant.java:62)
at org.jooq.impl.DSL.val(DSL.java:12781)
at org.jooq.impl.DSL.val(DSL.java:12732)
at org.jooq.impl.DSL.inline(DSL.java:12590)
at org.jooq.impl.UDTRecordImpl.toString(UDTRecordImpl.java:141)
at java.lang.String.valueOf(String.java:2994)
at org.jooq.impl.AbstractParam.name(AbstractParam.java:104)
at org.jooq.impl.AbstractParam.<init>(AbstractParam.java:78)
at org.jooq.impl.AbstractParam.<init>(AbstractParam.java:74)
at org.jooq.impl.UDTConstant.<init>(UDTConstant.java:62)
Any idea what I am doing wrong?
This is an issue that exists in older versions of Jooq 3.7.0. For projects who are using that version and cannot migrate easily to newer versions, this question might be a starting point, so I am not voting to close it (other's are requested to if they see fit).
Upgrading to a later version of jooq fixes this issue.

getStructureId returns the real ID minus 1?

this is the code I am using :
JournalArticle article = null;
article = JournalArticleLocalServiceUtil.getLatestArticle(classPk);
String structureId = article.getStructureId();
When I debugged I found that structureId is always the real structureId but minus 1 !!!
Why ?? I need to know if it's Liferay bug ...
thank you,
I am Liferay 6.2 ce ga2.
JournalArticle's field structureId is equivalent not to DDMStructure.structureId, but to DDMStructure.structureKey. I admit, that can be really confusing.
This is due to the DDMStructure's object generation mechanism. When you add new structure using Control Panel, the structureKey is generated automatically using counterLocalService (check this code). As it happens just before the structureId is generated, it is always smaller by one.
See following Jira tickets, where it is explained the structureId vs structureKey issue:
- https://issues.liferay.com/browse/LPS-50671
- https://issues.liferay.com/browse/LPS-31163

Moped::Errors::OperationFailure when creating an embedded object

I'm using mongoid 3.1.4 altogether with moped 1.5.1, mongodb 2.4.1, and ruby 1.9.3.
I have next models:
class Practice
include Mongoid::Document
embeds_many :distresses
end
class Distress
include Mongoid::Document
embedded_in :practice
end
When I do something like this it seems to be working:
practice = Practice.create
practice.distresses.create
But when I place safe: true in my config file and I do the same, then I get:
Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
#length=82
#request_id=22
#response_to=0
#op_code=2004
#flags=[]
#full_collection_name="collection.$cmd"
#skip=0
#limit=-1
#selector={:getlasterror=>1, :safe=>true}
#fields=nil>
And actually, I got the error when creating the distress in any way. This also throws the exception:
practice = Practice.create
distress = practice.distresses.build
distress.save
When I check with practice.distresses.count I can see that distresses where created successfully in the database, however I get the exception mentioned above.
Ok, after some days I was able to fix this problem.
In my Distress model I had a before_create callback that was trying to update a field on the Practice parent object. Somehow this makes Moped to create a wrong request that makes MongoDB to fail.
I changed before_create callback for after_create and everything is working now.
Hope this helps somebody else.

when updating item with changed embedded docs in Mongoose, these embedded docs don't update

I'm doing the following using Mongoose:
that.model.update({_id: dao._id}, dao, { upsert: true }, cb);
Where dao is a mongoose representation containing (among other things) a couple of embedded documents. As a test I've deleted a couple of the embedded docs from the array before calling the update-method above.
The result is that the change to the array of embedded docs IS NOT persisted.
Anything I'm overlooking?
Hard to be certain w/o seeing more code, but if dao is a Mongoose model instance, you should be calling dao.save(cb); instead.
I solved the problem by doing something similar as proposed in the following issue: https://github.com/LearnBoost/mongoose/issues/571
For completeness some background which led to the problem.
I'm using DDD repositories which are populated on app-start. Under the hood this fetches Mongoose-objects (which are treate as DAOs in my situation) and are translated to domainobjects, which are cached in the repository. I need this separation between domainobjects and mongoose-objects, don't ask.
This means that getById, getAll and all other public interfaces of the repo work with domainobjects and not with mongoose-objects.
When doing things like add or update on the repo this internally only updates the in-mem cache (which, again, only uses domainobjects instead of mongoose-objects)
Only when doing commit on the repo does the possibly changed collection of domainobjects get persisted. This is done by creating NEW Mongoose-objects instead of fetching Existing mongoose-objects and updating those.
This is why I can't use dao.save() since, when I'm saving a different (just created) mongoose-object while a mongoose-object with the same id may possibly already exist in Mongo, it throws a duplicate id error.
Some relevant snippet from by code illustrating the solution:
var dao = that.createDAO(domainobject);
//https://github.com/LearnBoost/mongoose/issues/571
// Convert the Model instance to a simple object using Model's 'toObject' function
// to prevent weirdness like infinite looping...
var upsertData = dao.toObject();
// Delete the _id property, otherwise Mongo will return a "Mod on _id not allowed" error
delete upsertData._id;
that.model.update({_id: dao._id}, upsertData, { upsert: true }, cb);