MongoDB: Find a document by non-existence of a field?

ID : 20434

viewed : 11

Tags : mongodbmongodb

Top 5 Answer for MongoDB: Find a document by non-existence of a field?

vote vote

95

Try the $exists operator:

db.mycollection.find({ "price" : { "$exists" : false } }) 

and see its documentation.

vote vote

83

vote vote

79

I had a similar problem but found the issue was that I had neglected to provide a default constructor for the DTO that was annotated with @RequestBody.

vote vote

65

I faced a similar issue and this is how I fixed it,

The problem is due to the conversion process from JSON to Java, one need to have the right run time jackson libraries for the conversion to happen correctly.

Add the following jars (through dependency or by downloading and adding to the classpath.

<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency> 

This should fix the problem.

Complete Code:

function() {   $.ajax({     type: "POST",     url: "saveUserDetails.do",     data: JSON.stringify({       name: "Gerry",       ity: "Sydney"     }),     headers: {       'Accept': 'application/json',       'Content-Type': 'application/json'     },     success: function(data) {       if (data.status == 'OK')         alert('Person has been added');       else         alert('Failed adding person: ' + data.status + ', ' + data.errorMessage); } 

and the controller signature looks like this:

@RequestMapping(value = "/saveUserDetails.do", method = RequestMethod.POST) public @ResponseBody Person addPerson( @RequestBody final  Person person) { 

Hope this helps

vote vote

53

I believe I ran exactly into the same issue. After countless hours of fighting with the JSON, the JavaScript and the Server, I found the culprit: In my case I had a Date object in the DTO, this Date object was converted to a String so we could show it in the view with the format: HH:mm.

When JSON information was being sent back, this Date String object had to be converted back into a full Date Object, therefore we also need a method to set it in the DTO. The big BUT is you cannot have 2 methods with the same name (Overload) in the DTO even if they have different type of parameter (String vs Date) because this will give you also the 415 Unsupported Media type error.

This was my controller method

  @RequestMapping(value = "/alarmdownload/update", produces = "application/json", method = RequestMethod.POST)   public @ResponseBody   StatusResponse update(@RequestBody AlarmDownloadDTO[] rowList) {     System.out.println("hola");     return new StatusResponse();   } 

This was my DTO example (id get/set and preAlarm get Methods are not included for code shortness):

@JsonIgnoreProperties(ignoreUnknown = true) public class AlarmDownloadDTO implements Serializable {    private static final SimpleDateFormat formatHHmm = new SimpleDateFormat("HH:mm");    private String id;   private Date preAlarm;    public void setPreAlarm(Date date) {      this.preAlarm == date;   }   public void setPreAlarm(String date) {         try {       this.preAlarm = formatHHmm.parse(date);     } catch (ParseException e) {       this.preAlarm = null;     } catch (NullPointerException e){       this.preAlarm = null;     }   } } 

To make everything work you need to remove the method with Date type parameter. This error is very frustrating. Hope this can save someone hours of debugging.

Top 3 video Explaining MongoDB: Find a document by non-existence of a field?

Related QUESTION?