是的,可以强制转换MongoDB查询-
db.yourCollectionName.find("this.yourFieldName >yourValue);
为了理解这个概念,让我们用文档创建一个集合。使用文档创建集合的查询如下-
> db.castingDemo.insertOne({"Amount":"200"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e874cf1f7a64fa4df42") } > db.castingDemo.insertOne({"Amount":"100"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e8e4cf1f7a64fa4df43") } > db.castingDemo.insertOne({"Amount":"110"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e944cf1f7a64fa4df44") } > db.castingDemo.insertOne({"Amount":"95"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e9d4cf1f7a64fa4df45") } > db.castingDemo.insertOne({"Amount":"85"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947ea44cf1f7a64fa4df46") } > db.castingDemo.insertOne({"Amount":"75"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947ebd4cf1f7a64fa4df47") }
在find()
method的帮助下显示集合中的所有文档。查询如下-
> db.castingDemo.find().pretty();
以下是输出-
{ "_id" : ObjectId("5c947e874cf1f7a64fa4df42"), "Amount" : "200" } { "_id" : ObjectId("5c947e8e4cf1f7a64fa4df43"), "Amount" : "100" } { "_id" : ObjectId("5c947e944cf1f7a64fa4df44"), "Amount" : "110" } { "_id" : ObjectId("5c947e9d4cf1f7a64fa4df45"), "Amount" : "95" } { "_id" : ObjectId("5c947ea44cf1f7a64fa4df46"), "Amount" : "85" } { "_id" : ObjectId("5c947ebd4cf1f7a64fa4df47"), "Amount" : "75" }
这是自动从字符串转换为数字的查询-
> db.castingDemo.find("this.Amount > 85");
以下是输出-
{ "_id" : ObjectId("5c947e874cf1f7a64fa4df42"), "Amount" : "200" } { "_id" : ObjectId("5c947e8e4cf1f7a64fa4df43"), "Amount" : "100" } { "_id" : ObjectId("5c947e944cf1f7a64fa4df44"), "Amount" : "110" } { "_id" : ObjectId("5c947e9d4cf1f7a64fa4df45"), "Amount" : "95" }