Reputation: 6187
I have query for deletion like so:
Query<E> query = datastore.createQuery(entityClass);
query.field(fieldName).equals(fieldValue);
return datastore.findAndDelete(query);
So the intent is to select an object based on an unique field and have it deleted. I have four objects in the collection, each with a different value for the field I'm querying by. But when I perform the findAndDelete
a random object is deleted returned, not the one matching the criteria.
I execute this over and over and each time another random object is returned, until all are deleted and then it returns null
.
I'm essentially executing this:
Query<Entity> query = datastore.createQuery(Entity.class);
query.field("uniqueHash").equals("SDFSDGRTGEFQDFRYDGSWE");
return datastore.findAndDelete(query);
And when I debug I see that all objects in the collections actually have different values for the uniqueHash
field
What am I missing?? Am I not building this query correctly?
**UPDATE: ** Here is the complete data before and after the delete operation:
> db.CustomObject.find().pretty()
{
"_id" : ObjectId("54a590546c66e6672cfff53a"),
"className" : "com.test.CustomObject",
"tokenHash" : "SG6o3mXOOBfGHYlhA2S2p52O1/GXOUZvF6kY5X3ZyvA="
}
{
"_id" : ObjectId("54a590546c66e6672cfff53b"),
"className" : "com.test.CustomObject",
"tokenHash" : "RotU72A4E9Oh8qC+cCX4Y+MdIShuuaDX03jQQXZVG34="
}
{
"_id" : ObjectId("54a590546c66e6672cfff53c"),
"className" : "com.test.CustomObject",
"tokenHash" : "sYLKPRlddqc3I+ORVwCzYjzeU+ErBveQNvHYYyCsi7o="
}
{
"_id" : ObjectId("54a590546c66e6672cfff53d"),
"className" : "com.test.CustomObject",
"tokenHash" : "eVltEP/ptPqw6WrMDCCzd+rcoVrShcMr9fF3CENLj8Q="
}
Then I execute:
Query<CustomObject> query = datastore.createQuery(CustomObject.class);
query.field("tokenHash").equals("RotU72A4E9Oh8qC+cCX4Y+MdIShuuaDX03jQQXZVG34=");
return datastore.findAndDelete(query);
And the result is that an object was deleted, but not the one I was hoping for:
> db.CustomObject.find().pretty()
{
"_id" : ObjectId("54a590546c66e6672cfff53b"),
"className" : "com.test.CustomObject",
"tokenHash" : "RotU72A4E9Oh8qC+cCX4Y+MdIShuuaDX03jQQXZVG34="
}
{
"_id" : ObjectId("54a590546c66e6672cfff53c"),
"className" : "com.test.CustomObject",
"tokenHash" : "sYLKPRlddqc3I+ORVwCzYjzeU+ErBveQNvHYYyCsi7o="
}
{
"_id" : ObjectId("54a590546c66e6672cfff53d"),
"className" : "com.test.CustomObject",
"tokenHash" : "eVltEP/ptPqw6WrMDCCzd+rcoVrShcMr9fF3CENLj8Q="
}
Thanks!
Upvotes: 2
Views: 2374
Reputation: 19700
You need to use the Field
class' equal
method instead of the equals
method of the Object
class.
query.field("tokenHash").equal("RotU72A4E9Oh8qC+cCX4Y+MdIShuuaDX03jQQXZVG34=");
equals
is a method in the Object
class used for comparing the value in two Objects. It returns just true or false. Hence the below statement
query.field("tokenHash").equals("RotU72A4E9Oh8qC+cCX4Y+MdIShuuaDX03jQQXZVG34=");
Would return false
, without actually building an query
object which the equal
method of the Field
class does.
Upvotes: 2