Query records

更新时间:
复制 MD 格式

This topic describes how to query records in a dataset using the data storage service.

Client invocation

  • Query a single record.

    Call the findOne method to query the name of the record with the lowest age value greater than 18.

    projection: { name: 1 } specifies that only the name field is returned. In this context, a value of 1 includes the field, and a value of 0 excludes it.

    sort: { age: 1 } sorts the query results in ascending order. A value of 1 specifies ascending order, and -1 specifies descending order.

    mpserverless.db.collection('users')
      .findOne({
        age: { $gt: 18 }
      }, {
        projection: { name: 1 },
        sort: { age: 1 }
      })
      .then(res => { })
      .catch(console.error);
  • Query all records.

    Call the find method to query the name of all records with an age greater than 18. The results are returned in ascending order.

    mpserverless.db.collection('users')
      .find({
        age: { $gt: 18 }
      }, {
        projection: { name: 1 },
        sort: { age: 1 }
      })
      .then(res => { })
      .catch(console.error);
  • Query and delete a single record.

    Call the findOneAndDelete method to find and delete the record with the highest age value that is less than 18.

    mpserverless.db.collection('users')
      .findOneAndDelete({
        age: { $lt: 18 }
      }, {
        sort: { age: -1 }
      })
      .then(res => { })
      .catch(console.error);
  • Query and replace a single record.

    Call the findOneAndReplace method to find and replace a document whose name is zhangsan.

    mpserverless.db.collection('users')
      .findOneAndReplace({
        name: "john"
      }, {
        $set: {
          name: "lisa",
          age: 20
        }
      })
      .then(res => { })
      .catch(console.error);
  • Query and update a record.

    Call findOneAndUpdate to update the age field of a record where the name is zhangsan.

    mpserverless.db.collection('users')
        .findOneAndUpdate({
            name: "john"
        },
        {
        $set: {
            age: 18
            }
        })
        .then(res => {})
        .catch(console.error);

Function invocation

  • Query a single record.

    Call the findOne method to query the name of the record with the lowest age value greater than 18.

    projection: { name: 1 } specifies that only the name field is returned. In this context, a value of 1 includes the field, and a value of 0 excludes it.

    sort: { age: 1 } sorts the query results in ascending order. A value of 1 specifies ascending order, and -1 specifies descending order.

    'use strict';
    
    module.exports = async function (ctx) {
      return await ctx.mpserverless.db.collection('users')
        .findOne({
          age: { $gt: 18 }
        }, {
          projection: { name: 1 },
          sort: { age: 1 }
        });
    };                    
  • Query all records.

    Call the find method to query the name of all records with an age greater than 18. The results are returned in ascending order.

    'use strict';
    
    module.exports = async function (ctx) {
      return await ctx.mpserverless.db.collection('users')
        .find({
          age: { $gt: 18 }
        }, {
          projection: { name: 1 },
          sort: { age: 1 }
        });
    };
  • Query and delete a single record.

    Call the findOneAndDelete method to find and delete the record with the highest age value that is less than 18.

    'use strict';
    
    module.exports = async function (ctx) {
      return await ctx.mpserverless.db.collection('users')
        .findOneAndDelete({
          age: { $lt: 18 }
        }, {
          sort: { age: -1 }
        });
    };                    
  • Query and replace a single record.

    You can call the findOneAndReplace method to find and replace a document whose name field is zhangsan.

    'use strict';
    
    module.exports = async function (ctx) {
      return await ctx.mpserverless.db.collection('users')
        .findOneAndReplace({
          name: "john"
        },{
          $set: {
            name: "lisa",
            age: 20
          }
        });
    }; 
  • Query and update a record.

    Call findOneAndUpdate to update the age field of a record where the name field is zhangsan.

    'use strict';
    
    module.exports = async function (ctx) {
      return await ctx.mpserverless.db.collection('users')
        .findOneAndUpdate({
            name: "john"
        },
          {
            $set: {
              age: 18
            }
          });
    };