Cloud Firestore มีฟังก์ชันการค้นหาที่มีประสิทธิภาพสำหรับการระบุเอกสารที่คุณต้องการดึงข้อมูลจากคอลเล็กชัน ข้อความค้นหาเหล่านี้ยังสามารถใช้กับ get()
หรือ addSnapshotListener()
ดังที่อธิบายไว้ใน Get Data
สั่งซื้อและจำกัดข้อมูล
ตามค่าเริ่มต้น แบบสอบถามจะดึงเอกสารทั้งหมดที่ตรงกับแบบสอบถามโดยเรียงลำดับจากน้อยไปหามากตามรหัสเอกสาร คุณสามารถระบุลำดับการจัดเรียงสำหรับข้อมูลของคุณโดยใช้ orderBy()
และคุณสามารถจำกัดจำนวนเอกสารที่ดึงข้อมูลโดยใช้ limit()
หากคุณระบุ limit()
ค่าจะต้องมากกว่าหรือเท่ากับศูนย์
ตัวอย่างเช่น คุณสามารถค้นหา 3 เมืองแรกตามลำดับตัวอักษรด้วย:
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web namespaced API
citiesRef.orderBy("name").limit(3);
สวิฟท์
citiesRef.order(by: "name").limit(to: 3)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Kotlin+KTX
citiesRef.orderBy("name").limit(3)
Java
citiesRef.orderBy("name").limit(3);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
ชวา
หลาม
Python
ซี++
cities_ref.OrderBy("name").Limit(3);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("Name").Limit(3);
ค#
ทับทิม
คุณสามารถเรียงลำดับจากมากไปน้อยเพื่อให้ได้ 3 เมือง สุดท้าย :
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web namespaced API
citiesRef.orderBy("name", "desc").limit(3);
สวิฟท์
citiesRef.order(by: "name", descending: true).limit(to: 3)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
Kotlin+KTX
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
Java
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descending: true).limit(3);
ชวา
หลาม
Python
ซี++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderByDescending("Name").Limit(3);
ค#
ทับทิม
คุณยังสามารถสั่งซื้อได้หลายช่อง ตัวอย่างเช่น หากคุณต้องการเรียงลำดับตามรัฐ และภายในแต่ละรัฐเรียงลำดับตามจำนวนประชากรจากมากไปน้อย:
Web modular API
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web namespaced API
citiesRef.orderBy("state").orderBy("population", "desc");
สวิฟท์
citiesRef .order(by: "state") .order(by: "population", descending: true)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
Kotlin+KTX
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
Java
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("population", descending: true);
ชวา
หลาม
Python
ซี++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
ค#
ทับทิม
คุณสามารถรวมตัวกรอง where()
เข้ากับ orderBy()
และ limit()
ได้ ในตัวอย่างต่อไปนี้ คิวรีจะกำหนดเกณฑ์ประชากร จัดเรียงตามจำนวนประชากรจากน้อยไปมาก และส่งกลับเฉพาะผลลัพธ์สองสามรายการที่เกินขีดจำกัดเท่านั้น:
Web modular API
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
สวิฟท์
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
วัตถุประสงค์-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Dart
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("population") .limit(2);
ชวา
หลาม
Python
ซี++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
ค#
ทับทิม
อย่างไรก็ตาม หากคุณมีตัวกรองที่มีการเปรียบเทียบช่วง ( <
, <=
, >
, >=
) การเรียงลำดับครั้งแรกของคุณต้องอยู่ในฟิลด์เดียวกัน ดูรายการข้อจำกัด orderBy()
ด้านล่าง
ข้อจำกัด
สังเกตข้อจำกัดต่อไปนี้สำหรับคำ orderBy()
:
- ส่วนคำสั่ง
orderBy()
ยัง กรองการมีอยู่ของฟิลด์ที่กำหนด ด้วย ชุดผลลัพธ์จะไม่รวมเอกสารที่ไม่มีฟิลด์ที่กำหนด หากคุณรวมตัวกรองที่มีการเปรียบเทียบช่วง (
<
,<=
,>
,>=
) การสั่งซื้อครั้งแรกของคุณต้องอยู่ในฟิลด์เดียวกัน:Valid : ตัวกรองช่วงและ
orderBy
บนฟิลด์เดียวกันWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("population");
สวิฟท์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App ClipcitiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App Clip[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population")
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
ชวา
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
หลาม
Python
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
ค#
ทับทิม
ไม่ถูกต้อง : ตัวกรองช่วงและ
orderBy
แรกตามในฟิลด์ที่ต่างกันWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("country");
สวิฟท์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App ClipcitiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App Clip[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("country")
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
ชวา
หลาม
Python
ซี++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
ค#
ทับทิม
orderBy
และการดำรงอยู่
เมื่อคุณเรียงลำดับการสืบค้นตามฟิลด์ที่กำหนด การสืบค้นจะสามารถส่งคืนเฉพาะเอกสารที่มีฟิลด์การเรียงลำดับตามอยู่เท่านั้น
ตัวอย่างเช่น คำค้นหาต่อไปนี้จะไม่ส่งคืนเอกสารใดๆ ที่ไม่ได้ตั้งค่าช่อง population
แม้ว่าเอกสารเหล่านั้นจะตรงตามตัวกรองคำค้นหาก็ตาม
ชวา
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
ผลกระทบที่เกี่ยวข้องนำไปใช้กับความไม่เท่าเทียมกัน แบบสอบถามที่มีตัวกรองความไม่เท่าเทียมกันในเขตข้อมูลยังหมายถึงการเรียงลำดับตามเขตข้อมูลนั้นด้วย แบบสอบถามต่อไปนี้ไม่ส่งคืนเอกสารที่ไม่มีฟิลด์ population
แม้ว่า country = USA
ในเอกสารนั้นก็ตาม วิธีแก้ปัญหาชั่วคราว คุณสามารถดำเนินการสอบถามแยกกันสำหรับการสั่งซื้อแต่ละรายการ หรือคุณสามารถกำหนดค่าให้กับฟิลด์ทั้งหมดที่คุณสั่งซื้อได้
ชวา
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
ข้อความค้นหาด้านบนมีการเรียงลำดับโดยนัยเกี่ยวกับความไม่เท่าเทียมกันและเทียบเท่ากับข้อความต่อไปนี้:
ชวา
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);
Cloud Firestore มีฟังก์ชันการค้นหาที่มีประสิทธิภาพสำหรับการระบุเอกสารที่คุณต้องการดึงข้อมูลจากคอลเล็กชัน ข้อความค้นหาเหล่านี้ยังสามารถใช้กับ get()
หรือ addSnapshotListener()
ดังที่อธิบายไว้ใน Get Data
สั่งซื้อและจำกัดข้อมูล
ตามค่าเริ่มต้น แบบสอบถามจะดึงเอกสารทั้งหมดที่ตรงกับแบบสอบถามโดยเรียงลำดับจากน้อยไปหามากตามรหัสเอกสาร คุณสามารถระบุลำดับการจัดเรียงสำหรับข้อมูลของคุณโดยใช้ orderBy()
และคุณสามารถจำกัดจำนวนเอกสารที่ดึงข้อมูลโดยใช้ limit()
หากคุณระบุ limit()
ค่าจะต้องมากกว่าหรือเท่ากับศูนย์
ตัวอย่างเช่น คุณสามารถค้นหา 3 เมืองแรกตามลำดับตัวอักษรด้วย:
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web namespaced API
citiesRef.orderBy("name").limit(3);
สวิฟท์
citiesRef.order(by: "name").limit(to: 3)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Kotlin+KTX
citiesRef.orderBy("name").limit(3)
Java
citiesRef.orderBy("name").limit(3);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
ชวา
หลาม
Python
ซี++
cities_ref.OrderBy("name").Limit(3);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("Name").Limit(3);
ค#
ทับทิม
คุณสามารถเรียงลำดับจากมากไปน้อยเพื่อให้ได้ 3 เมือง สุดท้าย :
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web namespaced API
citiesRef.orderBy("name", "desc").limit(3);
สวิฟท์
citiesRef.order(by: "name", descending: true).limit(to: 3)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
Kotlin+KTX
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
Java
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descending: true).limit(3);
ชวา
หลาม
Python
ซี++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderByDescending("Name").Limit(3);
ค#
ทับทิม
คุณยังสามารถสั่งซื้อได้หลายช่อง ตัวอย่างเช่น หากคุณต้องการเรียงลำดับตามรัฐ และภายในแต่ละรัฐเรียงลำดับตามจำนวนประชากรจากมากไปน้อย:
Web modular API
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web namespaced API
citiesRef.orderBy("state").orderBy("population", "desc");
สวิฟท์
citiesRef .order(by: "state") .order(by: "population", descending: true)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
Kotlin+KTX
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
Java
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("population", descending: true);
ชวา
หลาม
Python
ซี++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
ค#
ทับทิม
คุณสามารถรวมตัวกรอง where()
เข้ากับ orderBy()
และ limit()
ได้ ในตัวอย่างต่อไปนี้ คิวรีจะกำหนดเกณฑ์ประชากร จัดเรียงตามจำนวนประชากรจากน้อยไปมาก และส่งกลับเฉพาะผลลัพธ์สองสามรายการที่เกินขีดจำกัดเท่านั้น:
Web modular API
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
สวิฟท์
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
วัตถุประสงค์-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Dart
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("population") .limit(2);
ชวา
หลาม
Python
ซี++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
ค#
ทับทิม
อย่างไรก็ตาม หากคุณมีตัวกรองที่มีการเปรียบเทียบช่วง ( <
, <=
, >
, >=
) การเรียงลำดับครั้งแรกของคุณต้องอยู่ในฟิลด์เดียวกัน ดูรายการข้อจำกัด orderBy()
ด้านล่าง
ข้อจำกัด
สังเกตข้อจำกัดต่อไปนี้สำหรับคำ orderBy()
:
- ส่วนคำสั่ง
orderBy()
ยัง กรองการมีอยู่ของฟิลด์ที่กำหนด ด้วย ชุดผลลัพธ์จะไม่รวมเอกสารที่ไม่มีฟิลด์ที่กำหนด หากคุณรวมตัวกรองที่มีการเปรียบเทียบช่วง (
<
,<=
,>
,>=
) การสั่งซื้อครั้งแรกของคุณต้องอยู่ในฟิลด์เดียวกัน:Valid : ตัวกรองช่วงและ
orderBy
บนฟิลด์เดียวกันWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("population");
สวิฟท์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App ClipcitiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App Clip[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population")
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
ชวา
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
หลาม
Python
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
ค#
ทับทิม
ไม่ถูกต้อง : ตัวกรองช่วงและ
orderBy
แรกตามในฟิลด์ที่ต่างกันWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("country");
สวิฟท์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App ClipcitiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้งานได้บนเป้าหมาย watchOS และ App Clip[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("country")
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
ชวา
หลาม
Python
ซี++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
โหนด js
ไป
PHP
PHP
หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
ค#
ทับทิม
orderBy
และการดำรงอยู่
เมื่อคุณเรียงลำดับการสืบค้นตามฟิลด์ที่กำหนด การสืบค้นจะสามารถส่งคืนเฉพาะเอกสารที่มีฟิลด์การเรียงลำดับตามอยู่เท่านั้น
ตัวอย่างเช่น คำค้นหาต่อไปนี้จะไม่ส่งคืนเอกสารใดๆ ที่ไม่ได้ตั้งค่าช่อง population
แม้ว่าเอกสารเหล่านั้นจะตรงตามตัวกรองคำค้นหาก็ตาม
ชวา
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
ผลกระทบที่เกี่ยวข้องนำไปใช้กับความไม่เท่าเทียมกัน แบบสอบถามที่มีตัวกรองความไม่เท่าเทียมกันในเขตข้อมูลยังหมายถึงการเรียงลำดับตามเขตข้อมูลนั้นด้วย แบบสอบถามต่อไปนี้ไม่ส่งคืนเอกสารที่ไม่มีฟิลด์ population
แม้ว่า country = USA
ในเอกสารนั้นก็ตาม วิธีแก้ปัญหาชั่วคราว คุณสามารถดำเนินการสอบถามแยกกันสำหรับการสั่งซื้อแต่ละรายการ หรือคุณสามารถกำหนดค่าให้กับฟิลด์ทั้งหมดที่คุณสั่งซื้อได้
ชวา
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
ข้อความค้นหาด้านบนมีการเรียงลำดับโดยนัยเกี่ยวกับความไม่เท่าเทียมกันและเทียบเท่ากับข้อความต่อไปนี้:
ชวา
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);