74817

how to check if an document is updated or inserted in MongoDB

Question:

I'm just wondering if there's way to check if a given document is updated or inserted in MongoDB after a upsert operation in Java:

DBCollection col = getCollection(); // either this col.findAndModify(query, null, null, false, contact, false, true); // or this WriteResult wr = col.update(query, contact, true, false);

Answer1:

Generally, WriteResult will hold information regarding whether it was successful or not; if successful, the document will be there.

You could also do a simple col.count(new BasicDBObject("_id",{id of your document})) and check if the value is 1.

Answer2:

1-> step : set the <a href="http://www.mongodb.org/display/DOCS/Database+Profiler" rel="nofollow">profiler</a> using > db.setProfilingLevel(2); on the document target.<br /> 2-> run this query

db.system.profile.find({},{ts:1, op:1, ns:1}).sort({ts: -1}).forEach(printjson);

it will show you in the op attribute if the given document is updated or inserted and ns is the document name. ts when the query run.

Recommend

  • Incremental field to existing collection
  • how to remove empty line in a file in java
  • how to use findAndModify() function in mongo
  • Find and Modify with MongoDB C#
  • ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
  • Spring mvc: css does not work when adding slash at the end of URL
  • Spring Data: JPA Schema JpaRepositoryConfigDefinitionParser intiailization fails
  • Error - Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs)= etc
  • An SPI class of type org.apache.lucene.codecs.Codec with name 'Lucene54' does not exist
  • How to resolve “Ambiguous reference to a JS library”?
  • Struts2 simple Application not executing
  • Why does swagger annotations generate api-docs with default path prefix
  • Obtaining GUID for Existing USB Device on Windows XP
  • Restore session after restart in JBoss
  • JAXB 2 in an Oracle 10g Webapp
  • JENKINS: ERROR when I try to use an older JDK for a specific maven project
  • HttpServletRequest getPart(\") not found
  • Duplicated password validation messages ONLY IF new password is 1 char long
  • How to force refresh on CallLog.Calls.CACHED_NAME column?
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • Submission of new app with iAds
  • How do I refer to a client_deploy.wsdd file that's in WEB-INF?
  • testing a POST using phpunit in laravel 4
  • F#: In which memory area is the continuation stored: stack or heap?
  • Most efficient way to move table rows from one table to another
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • what is the difference between the asp.net mvc application and asp.net web application
  • Display Images one by one with next and previous functionality
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • SQL merge duplicate rows and join values that are different
  • How to stop GridView from loading again when I press back button?
  • LevelDB C iterator
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?