Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs 菜单
Docs 主页
/ /

$gte(查询谓词运算操作符)

$gte

$gte 选择指定字段的值大于或等于 (>=) 指定值的文档。

对于大多数数据类型, 比较运算符仅对BSON类型与查询值的类型匹配的字段执行比较。 MongoDB通过类型范围支持有限的跨BSON比较。

可以使用 $gte 查找托管在以下环境中的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

$gte 操作符采用以下形式:

{ field: { $gte: value } }

本页上的示例使用sample_mflix示例数据集中的数据。有关如何将此数据集加载到自管理MongoDB 部署中的详细信息,请参阅加载示例数据集。如果对示例数据库进行了任何修改,则可能需要删除并重新创建数据库才能运行本页上的示例。

此示例选择 movies集合中 runtime 大于或等于 720 分钟的document:

db.movies.find(
{ "runtime": { $gte: 720 } },
{ _id: 0, title: 1, runtime: 1, plot: 1 }
)
[
{
plot: 'The economic and cultural growth of Colorado spanning two centuries from the mid-1700s to the late-1970s.',
runtime: 1256,
title: 'Centennial'
},
{
plot: 'A dramatization of the missions and adventures of the greatest spy in British history.',
runtime: 720,
title: 'Reilly: Ace of Spies'
},
{
plot: "A 13-hour mini-series detailing James A. Michner's fictional account of the American space program from the years after World War II to the Apollo landings on the moon in the early 1970's.",
runtime: 780,
title: 'Space'
},
{
plot: 'A documentary on the history of the sport with major topics including Afro-American players, player/team owner relations and the resilience of the game.',
runtime: 1140,
title: 'Baseball'
},
{
plot: 'Taken spans five decades and four generations, centering on three families: the Keys, Crawfords, and Clarkes. World War II veteran Russell Keys is plagued by nightmares of his abduction by ...',
runtime: 877,
title: 'Taken'
}
]

updateMany() 操作与名为 imdb 的嵌入式文档以及名为 rating 的子字段相匹配。它会在 rating 大于或等于 9.5 的每个 document 中设置 { highestRated: true }

db.movies.updateMany(
{ "imdb.rating" : { $gte: 9.5 } },
{ $set: { "highestRated": true } }
)
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}

要仅在imdb.rating大于 9.5 的第一个文档中设置 higestRated 字段,请使用 updateOne()

后退

$gt

在此页面上