DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Find Verified User in MongoDB


Find a user whose email is verified. The "emails" field is an array. The function of "$elemMatch" operator is:

Find a element in an array (here is emails), whose feature obey the value after "$elemMatch" (here its "verified" key is "true").

The code:

> db.users.findOne({'emails': {$elemMatch: {verified: true}}})
{
  "_id" : "rKawn5jCiuGcsPHdP",
  "createdAt" : ISODate("2015-05-07T01:04:23.375Z"),
  "services" : {
    "password" : {
      "bcrypt" : "$2a$10$T07N74DUrVrRtVGPT9dzleZlpRTlULfb9OrlWveW4NbcNpi.HkiAK"
    },
    "resume" : {
      "loginTokens" : [
        {
          "when" : ISODate("2015-05-07T01:04:23.902Z"),
          "hashedToken" : "Xbt2ASQF61artSmrYX1Gn6DLUNvbIrz9DW0sE4tbJh4="
        }
      ]
    },
    "email" : {
      "verificationTokens" : [ ]
    }
  },
  "emails" : [
    {
      "address" : "leechau@126.com",
      "verified" : true
    }
  ]
}


Published

May 7, 2015

Last Updated

May 7, 2015

Category

Tech

Tags

  • elemMatch 1
  • find 2
  • mongodb 24
  • users 2

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor