📜  Mongoose查询()函数

📅  最后修改于: 2022-05-13 01:56:22.999000             🧑  作者: Mango

Mongoose查询()函数

Query 构造函数用于构建查询。所以用户不需要直接实例化查询。 `query` 是 `Query` 的一个实例。

句法:

Query()

参数:此函数具有可选参数,如模型、条件、集合和选项。

返回值:此函数返回 Query 对象。

mongoose模块的安装:

  • 您可以访问安装mongoose模块的链接。您可以使用此命令安装此软件包。
npm install mongoose
  • 安装mongoose模块后,您可以使用命令在命令提示符下检查您的mongoose版本。
npm version mongoose
  • 之后,您可以创建一个文件夹并添加一个文件,例如 index.js,如下所示。

示例 1:文件名:index.js

javascript
const express = require('express');
const mongoose = require('mongoose');
const app = express()
  
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = new mongoose.Query();
console.log(query);
  
app.listen(3000, function(error ){
    if(error) console.log(error)
    console.log("Server listening on PORT 3000")
})


javascript
const mongoose = require('mongoose');
  
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = new mongoose.Query();
query.collection(User.collection);
console.log(query);


运行程序的步骤:

  • 项目结构将如下所示:

  • 使用以下命令运行index.js文件:
node index.js

输出:

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map {}, _posts: Map {} },
  _executionCount: 0,
  op: undefined,
  options: {},
  _conditions: {},
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection: undefined,
  _traceFunction: undefined,
  '$useProjection': true
}
Server listening on PORT 3000

示例 2:文件名:index.js

javascript

const mongoose = require('mongoose');
  
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = new mongoose.Query();
query.collection(User.collection);
console.log(query);

运行程序的步骤:

  • 项目结构将如下所示:

  • 使用以下命令运行index.js文件:
node index.js

输出:

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map {}, _posts: Map {} },
  _executionCount: 0,
  op: undefined,
  options: {},
  _conditions: {},
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection: NodeCollection {
    collection: NativeCollection {
      collection: null,
      Promise: [Function: Promise],
      _closed: false,
      opts: [Object],
      name: 'users',
      collectionName: 'users',
      conn: [NativeConnection],
      queue: [],
      buffer: true,
      emitter: [EventEmitter]
    },
    collectionName: 'users'
  },
  _traceFunction: undefined,
  '$useProjection': true
}

参考: https://mongoosejs.com/docs/api/query.html