如何从 mongodb 中的集合中仅检索一个特定文档
How to retrieve only one specific document from a collection in mongodb
我创建了一个名为“Cars”的数据库,并在其中创建了一个名为“Cars_info”的集合。我在里面插入了8个文件如下
db.Cars_info.insertMany([
{car_id: "c1", Company: "Toyota", Model: "Aqua", Year: 2020, Price_in_usd: 25000, Category: "High-end", Country: "Japan"},
{car_id: "c2", Company: "Toyota", Model: "Premio", Year: 2019, Price_in_usd: 35000, Category: "High-end", Country: "Japan"},
{car_id: "c3", Company: "Audi", Model: "A6", Year: 2020, Price_in_usd: 55000, Category: "High-end", Country: "Germany"},
{car_id: "c4", Company: "Tata", Model: "Nano", Year: 2015, Price_in_usd: 10000, Category: "Low-end", Country: "India"},
{car_id: "c5", Company: "Volkswagen", Model: "Taos", Year: 2022, Price_in_usd: 35000, Category: "High-end", Country: "Germany"},
{car_id: "c6", Company: "Ford", Model: "Figo", Year: 2019, Price_in_usd: 26000, Category: "High-end", Country: "America"},
{car_id: "c7", Company: "Mahindra", Model: "Thar", Year: 2018, Price_in_usd: 18000, Category: "Low-end", Country: "India"},
{car_id: "c8", Company: "Honda", Model: "Vezel", Year: 2015, Price_in_usd: 33000, Category: "High-end", Country: "Japan"}
])
这里我只想从集合中检索第三个文档。但是没有匹配任何字段值。像,
db.Cars_info.find({"car_id":"c3"}).pretty()
有什么办法吗?
从索引开始取文档:2,只有1个文档,也就是第三个文档。
更新:感谢并感谢@Wernfried 指出,您需要.sort()
来保证获得文档的第n 个。对于您的场景,您必须按 car_id
.
排序
MongoDB does not store documents in a collection in a particular order. When sorting on a field that contains duplicate values, documents containing those values may be returned in any order.
db.Cars_info.find()
.sort({ "car_id": 1 })
.skip(2)
.limit(1)
我创建了一个名为“Cars”的数据库,并在其中创建了一个名为“Cars_info”的集合。我在里面插入了8个文件如下
db.Cars_info.insertMany([
{car_id: "c1", Company: "Toyota", Model: "Aqua", Year: 2020, Price_in_usd: 25000, Category: "High-end", Country: "Japan"},
{car_id: "c2", Company: "Toyota", Model: "Premio", Year: 2019, Price_in_usd: 35000, Category: "High-end", Country: "Japan"},
{car_id: "c3", Company: "Audi", Model: "A6", Year: 2020, Price_in_usd: 55000, Category: "High-end", Country: "Germany"},
{car_id: "c4", Company: "Tata", Model: "Nano", Year: 2015, Price_in_usd: 10000, Category: "Low-end", Country: "India"},
{car_id: "c5", Company: "Volkswagen", Model: "Taos", Year: 2022, Price_in_usd: 35000, Category: "High-end", Country: "Germany"},
{car_id: "c6", Company: "Ford", Model: "Figo", Year: 2019, Price_in_usd: 26000, Category: "High-end", Country: "America"},
{car_id: "c7", Company: "Mahindra", Model: "Thar", Year: 2018, Price_in_usd: 18000, Category: "Low-end", Country: "India"},
{car_id: "c8", Company: "Honda", Model: "Vezel", Year: 2015, Price_in_usd: 33000, Category: "High-end", Country: "Japan"}
])
这里我只想从集合中检索第三个文档。但是没有匹配任何字段值。像,
db.Cars_info.find({"car_id":"c3"}).pretty()
有什么办法吗?
从索引开始取文档:2,只有1个文档,也就是第三个文档。
更新:感谢并感谢@Wernfried 指出,您需要.sort()
来保证获得文档的第n 个。对于您的场景,您必须按 car_id
.
MongoDB does not store documents in a collection in a particular order. When sorting on a field that contains duplicate values, documents containing those values may be returned in any order.
db.Cars_info.find()
.sort({ "car_id": 1 })
.skip(2)
.limit(1)