在MongoDb中查找下一个文档

如果这是我的集合结构:</ p>

  {“_id  “:ObjectId(”4fdbaf608b446b0477000142“),
”name“:”product 1“,
},
{”_ id“:ObjectId(”fghfghfgjhjghkgk“),
”name“:”product 2“,\ n},
{“_ id”:ObjectId(“fghfghfgjhjghkgk”),
“name”:“product 3”,
}
</ code> </ pre>

和我 查询产品1,有没有办法查询下一个文件,在这种情况下是产品2?</ p>
</ div>

展开原文

原文

If this is my collection structure:

{   "_id": ObjectId("4fdbaf608b446b0477000142"),
    "name": "product 1",
},
{   "_id": ObjectId("fghfghfgjhjghkgk"),
    "name": "product 2",
},
{   "_id": ObjectId("fghfghfgjhjghkgk"),
    "name": "product 3",
}

and I query product 1, is there a way to query the next document, which in this case would be product 2?

dou2347
dou2347 我根本没有对它们进行重新排序,它们处于原始的插入顺序中。我得到产品1的方式是有人去testsite.com/4fdbaf608b446b0477000142我拿号码和id查询。
接近 8 年之前 回复
doucheng1891
doucheng1891 那么你想在“产品1”之后查询第一个文件?你如何定义文件的顺序(什么被认为是'之后')?
接近 8 年之前 回复

2个回答



如果您想要可预测的结果顺序,最好添加显式 sort()</ code>条件。</ p>

假设您所遵循的订单是“插入订单”并且您使用的是MongoDB的默认生成的ObjectIds,那么您可以根据ObjectId进行查询:</ p>

 <  code> //查找下一个创建的产品
db.products.find({_ id:{$ gt:ObjectId(“4fdbaf608b446b0477000142”)}})。limit(1)
</ code> </ pre>

请注意,此示例仅起作用,因为:</ p>


  • ObjectId的前四个字节是从unix样式的时间戳计算的(请参阅:ObjectId Specification )</ li>
  • _id </ code>的查询 单独使用默认的 _id </ code>索引(按id排序)来查找匹配</ li>
    </ ul>

    实际上,这种隐式排序与 :</ p>

      db.products.find({_ id:{$ gt:ObjectId(“4fdbaf608b446b0477000142”)}})。sort(  {_id:1})。limit(1); 
    </ code> </ pre>

    如果您为查询添加了更多条件以限定如何查找“下一个”产品(例如 , category </ code>),查询可能使用不同的索引,订单可能与您预期的不同。</ p>

    您可以使用 explain()。</ p>
    </ div>

展开原文

原文

It is best to add explicit sort() criteria if you want a predictable order of results.

Assuming the order you are after is "insertion order" and you are using MongoDB's default generated ObjectIds, then you can query based on the ObjectId:

// Find next product created
db.products.find({_id: {$gt: ObjectId("4fdbaf608b446b0477000142") }}).limit(1)

Note that this example only works because:

  • the first four bytes of the ObjectId are calculated from a unix-style timestamp (see: ObjectId Specification)
  • a query on _id alone will use the default _id index (sorted by id) to find a match

So really, this implicit sort is the same as:

db.products.find({_id: {$gt: ObjectId("4fdbaf608b446b0477000142" )}}).sort({_id:1}).limit(1);

If you added more criteria to the query to qualify how to find the "next" product (for example, a category), the query could use a different index and the order may not be as you expect.

You can check index usage with explain().



您可以使用ObjectId以插入顺序返回项目。 请参阅: http://www.mongodb.org/display/DOCS/Optimizing + Object + IDs#OptimizingObjectIDs </ p>

如果您想以其他顺序取回它们,则必须定义该订单的内容并在文档中存储更多信息。 </ p>
</ div>

展开原文

原文

You can get the items back in insertion order using ObjectId. See: http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs

If you want to get them back in some other order then you will have to define what that order is and store more information in your documents.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐