简体   繁体   中英

Check if property value exists

i'm looking for a way to check if a value does already exists. My current way is:

store.createQuery(MyClass.class).field(property).equal(value).countAll() > 0;

Is this the most efficient way or is there a better way to do this? I don't need count all, the first match is enough.

You seem to be really looking for the "createIfMissing" or general MongoDB "upsert" behavior. What this does is id the query condition does not find a document to update then a new document is created:

ops = store.createUpdateOperations(MyClass.class).set("name", "This Value");
store.updateFirst(MyClass.class).field(property).equal(value), ops, true)

Also look at the $set operator and .update() method from the core MongoDB documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM