查找数组字段为空的MongoDB记录

时间:2016-08-18 05:05:05

标签: mongodb mongodb-query

我需要输出一个不存在tdata birthdate的sketchid值列表/ null,只考虑每个tdata中的第一个生日。

每个集合中有很多字段,但我只对其中两个字段感兴趣:

{ 
  "sketchid": NumberLong(51148),
  "tdata": "[
    { "birthdate": ["529718400"], ...},
    { "birthdate": null, ... }
  ]
}

我需要为第一个生日为空的 tdata 打印 sketchid
注意:单个 tdata 包含许多 birthdate 字段,我只需要检查第一个 birthdate 字段,如果它是空的,我需要输出该集的 sketchid

我试过这个,但我无法得到解决方案。

db.timeline.find({tdata:{birthdate:{$exists: true,  “$ne”: null }}},{sketchid})

为了说明数据模式,find()命令(db.timeline.find().pretty())给出以下输出:

{
    "_id" : ObjectId("56ea7b9490e2a5b17c143b56"),
    "sketchid" : NumberLong(51147),
    "tdata" : "[{\\\"type\\\":\\\"dob\\\",\\\"title\\\":\\\"Vishakha Singh\\\",\\\"year\\\":null,\\\"color\\\":null,\\\"content\\\":\\\"\\\",\\\"birthdate\\\":[\\\"831254400\\\"],\\\"birthplace\\\":[\\\"Abu Dhabi, UAE\\\"],\\\"zodiac\\\":\\\"Taurus\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Cameo Role\\\",\\\"year\\\":[\\\"2014\\\"],\\\"color\\\":[\\\"#85330B\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Tamil movie Oru Oorla Rendu Raja"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Tamil movie Oru Oorla Rendu Raja<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Noticable Flick\\\",\\\"year\\\":[\\\"2013\\\"],\\\"color\\\":[\\\"#21C8F5\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Neetu in Fukrey"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Neetu in Fukrey<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Gained Recognition\\\",\\\"year\\\":[\\\"2010\\\"],\\\"color\\\":[\\\"#C90688\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Ashutosh Gowarikar\\'s Khelein Hum Jee Jaan Sey"]" data-sheets-userformat="[null,null,47617,[null,0],null,null,null,null,null,null,null,null,0,null,[null,2,721024],"sans-serif",11,null,1]">Ashutosh Gowarikar’s Khelein Hum Jee Jaan Sey<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Movie Debut\\\",\\\"year\\\":[\\\"2008\\\"],\\\"color\\\":[\\\"#FFCF00\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Hum Se Jahan"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Hum Se Jahan<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"quote-card\\\",\\\"title\\\":\\\"vishakha singh\\\",\\\"year\\\":null,\\\"color\\\":null,\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"I am not really sophisticated but happy and chilled out"]" data-sheets-userformat="[null,null,14915,[null,0],[null,2,16777215],null,null,null,null,0,null,null,0,null,[null,2,0],"helvetica",11]">I am not really sophisticated but happy and chilled out<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"}]"
}
{
    "_id" : ObjectId("56ea7b9490e2a5b17c143b57"),
    "sketchid" : NumberLong(51148),
    "tdata" : "[{\\\"type\\\":\\\"dob\\\",\\\"title\\\":\\\"Ali Fazal\\\",\\\"year\\\":null,\\\"color\\\":null,\\\"content\\\":\\\"\\\",\\\"birthdate\\\":[\\\"529718400\\\"],\\\"birthplace\\\":[\\\"Lucknow, Uttar Pradesh\\\"],\\\"zodiac\\\":\\\"Libra\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Bollywood Appearance\\\",\\\"year\\\":[\\\"2016\\\"],\\\"color\\\":[\\\"#C90688\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Anand L. Rai\\'s Happy Bhaag Jayegi"]" data-sheets-userformat="[null,null,47617,[null,0],null,null,null,null,null,null,null,null,0,null,[null,2,721024],"sans-serif",11,null,1]">Anand L. Rai’s Happy Bhaag Jayegi<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"English Movie\\\",\\\"year\\\":[\\\"2015\\\"],\\\"color\\\":[\\\"#FFCF00\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Special Appearance in Furious 7"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Special Appearance in Furious 7<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"First Lead Role\\\",\\\"year\\\":[\\\"2015\\\"],\\\"color\\\":[\\\"#B6D402\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Khamoshiyan"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Khamoshiyan<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Supporting Role\\\",\\\"year\\\":[\\\"2014\\\"],\\\"color\\\":[\\\"#FF2305\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Bobby Jasoos with Vidya Balan"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Bobby Jasoos with Vidya Balan<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"First Success\\\",\\\"year\\\":[\\\"2013\\\"],\\\"color\\\":[\\\"#85330B\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Fukrey"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Fukrey<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Bollywood Debut\\\",\\\"year\\\":[\\\"2009\\\"],\\\"color\\\":[\\\"#21C8F5\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Special Appearance in 3 Idiots"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Special Appearance in 3 Idiots<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"American Miniseries\\\",\\\"year\\\":[\\\"2009\\\"],\\\"color\\\":[\\\"#C90688\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Bollywood Hero"]" data-sheets-userformat="[null,null,513,[null,0],null,null,null,null,null,null,null,null,0]">Bollywood Hero<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"timeline\\\",\\\"title\\\":\\\"Screen Debut\\\",\\\"year\\\":[\\\"2008\\\"],\\\"color\\\":[\\\"#FFCF00\\\"],\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"Small role in the English language film The Other End of the Line"]" data-sheets-userformat="[null,null,309761,[null,0],null,null,null,null,null,null,null,null,0,null,[null,2,721024],"sans-serif",11,null,1,null,null,1]">Small role in the English language film The Other End of the Line<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"},{\\\"type\\\":\\\"quote-card\\\",\\\"title\\\":\\\"Ali Fazal\\\",\\\"year\\\":null,\\\"color\\\":null,\\\"content\\\":\\\"<p><span data-sheets-value="[null,2,"I love being in love."]" data-sheets-userformat="[null,null,14915,[null,0],[null,2,16777215],null,null,null,null,0,null,null,0,null,[null,2,4144959],"georgia",11]">I love being in love.<\\\\/span><\\\\/p>\\\\n\\\",\\\"birthdate\\\":null,\\\"birthplace\\\":null,\\\"zodiac\\\":\\\"Capricorn\\\"}]"
}

当我尝试执行以下命令时,我收到以下错误

  

db.timeline.find({tdata:{" birthdate":{$ exists:true,“$ ne”:null}}},{sketchid})   2016-08-19T12:02:14.665 + 0530 E QUERY [thread1]语法错误:非法字符@(shell):1:53

     

db.timeline.find({tdata:{" birthdate \\":{$ exists:true,“$ ne”:null}}},{sketchid})   2016-08-19T12:02:53.357 + 0530 E QUERY [thread1] SyntaxError:unterminated string literal @(shell):1:25

     

db.timeline.find({tdata:{\\" birthdate \\":{$ exists:true,“$ ne”:null}}},{sketchid})   2016-08-19T12:03:06.572 + 0530 E QUERY [thread1]语法错误:非法字符@(shell):1:25

1 个答案:

答案 0 :(得分:0)

如果只想测试tdata数组的第一个元素,则需要使用位置运算符。同样取决于您的版本{$ ne:null}并不总能给出您期望的内容。您可以检查null类型10:

db.timeline.find({"tdata.0.birthdate": {$exists:true,$not : { $type : 10 }} },{sketchid:1})