MongoDB查找嵌套元素 [英] MongoDB finding nested elements

查看:159
本文介绍了MongoDB查找嵌套元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MongoDB shell,我需要找到一些嵌套的元素。
我的结构如下:

  car:{$ ref:cars,$ id :{$ oid:4e8478ace4b0dea06288ad63}} 

我需要访问 oid = 4e8478ace4b0dea06288ad63



提前感谢



  {_id:{$ oid:4e7c6b68e4b0dea06288ad22},
driver:测试驱动程序,
category:BUSINESS,
distanceM:216.857,
mileageKmStart:121813.943,
mileageKmEnd:122030.8,
timestampStart:{$ date:2012-01-12T11:20:08Z},
timestampEnd:{$ date:2012-01-12T11:21:11Z},
car:{
$ ref:cars,
$ id:{$ oid:4e8478ace4b0dea06288ad63}
}
}


解决方案

p>

mongoexport 将对象ID表示为$ oid。你正在阅读由 mongoexport 生成的json文件吗?如果这是不是导入文档的结构(或者,它是,但是当你使用客户端谈话mongo,它不希望你使用这种语法)。



几乎可以肯定,你想要:

  db.x.find({'car。 $ id':ObjectId('4e8478ace4b0dea06288ad63')})

如果不是这样 - 看看mongo cli - 例如执行:

  db.x.findOne()

  db.x.findOne({'car':{$ exists:true}})//确保我发现有一个汽车

管理使用非法密钥创建/继承数据 - 您可能需要迁移其结构,以便可以查询您的数据。






DBRefs 是如何创建引用不同的集合 - 特别注意约定。它们不是特殊的(除了它们以任何方式被允许的键$ ref,$ id和$ db) - 它们只是另一个javascript对象mongo。


I am using the MongoDB shell and I need to find some nested elements. My structure looks like this:

 car : { "$ref" : "cars" , "$id" : { "$oid" : "4e8478ace4b0dea06288ad63"}}

I need to get to the oid = 4e8478ace4b0dea06288ad63

thanks in advance

Here is the structure :

{ "_id" : { "$oid" : "4e7c6b68e4b0dea06288ad22"} , 
  "driver" : "Test Driver" , 
  "category" : "BUSINESS" , 
  "distanceM" : 216.857 , 
  "mileageKmStart" : 121813.943 , 
  "mileageKmEnd" : 122030.8 , 
  "timestampStart" : { "$date" : "2012-01-12T11:20:08Z"} , 
  "timestampEnd" : { "$date" : "2012-01-12T11:21:11Z"} , 
  "car" : { 
      "$ref" : "cars" , 
      "$id" : { "$oid" : "4e8478ace4b0dea06288ad63"}
   }
}

解决方案

After a tiny dig around...

mongoexport will represent object ids as "$oid". Are you reading a json file generated by mongoexport? If so that is not the structure of your documents when imported (Or it is, but when you use a client to talk to mongo it does not want you to use this syntax).

Almost certainly, you want:

db.x.find({'car.$id': ObjectId('4e8478ace4b0dea06288ad63')})

If that isn't the case - have a look in the mongo cli - e.g. run:

db.x.findOne()

or

db.x.findOne({'car' : {$exists: true}}) // make sure the one I find has a car

If you've managed to create/inherit data with illegal keys - you'll probably want to migrate their structure so that it is possible to query your data.


DBRefs are a convention for how to create references to different collections - Note specifically "convention". They aren't special (except they are permitted the keys $ref, $id and $db) in any way - they are just another javascript object to mongo.

这篇关于MongoDB查找嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆