Personal summary, welcome to add, wrong pay off correction

This article mainly introduces how to operate the MongoDB database through Mongoose and share it with friends using Node

Mongoose basic use

1. Install

npm install mongoose --save
Copy the code

2. The link

//db.js
const mongoose=require('mongoose')

// Connect to MongODB database (nodebooks is the database name, please add your own username and password)
mongoose.connect("Mongo: / / 127.0.0.1 nodebooks", {useNewUrlParser:true},function(err){
			if (err) {
				console.log('Connection failed')}else{
				console.log('Connection successful')}})Copy the code

3. Data modeling

const mongoose=require('mongoose')

const Schema=mongoose.Schema;

const IdeaSchema=new Schema({
    user: {type:String./ / type
        default: 0./ / the default value
        required:true  // Mandatory
    },
    data: {type:Date.default:Date.now
    }
})
// Create the model
module.exports=mongoose.model('ideas',IdeaSchema) //'ideas' set name
Copy the code

4. Introduce the model and use it

// Import the js file for the link to the db as the second step
const db=require('.. /config/db')
// Import the model
require('./models/idea')
const idea=mongoose.model('ideas')
/ / query
idea.fine({})
Copy the code

The query

1. Query all results

find()
Copy the code

2. Specify which fields to return and which values not to return

// 1 indicates that the fields are returned. 0 indicates that the fields are not returned and can be used separately
find({},{"name":0."age":1})
Copy the code

3. Query by single condition

// Return all arrays that meet the criteria based on the _id query
find({_id:1})
// return the first character set that matches the condition based on the _id query (suitable for unique query)
findOne({_id:1})
Copy the code

4. Query multiple conditions

// Return all arrays that meet the criteria based on the _id query
find({_id:1.age:16})
// return the first character set that matches the condition based on the _id query (suitable for unique query)
findOne({_id:1.age:16})
` `'1. Query all results` `javascript
find()
Copy the code

5. Or conditional query

// Query either name=xiaoming or age=16
find({"$or": [{"name":"xiaoming"}, {"age": 16}]})
Copy the code

6. Query comparison conditions

// result of age greater than 100 or less than 10
find({"age": {"$gt":100."$lt":10}})
/** ** $gt is greater than $Gte is greater than $lt is less than $LTE is less than or equal to $NE is not equal to ** */ 
Copy the code

7. Include conditional query

// the name satisfies a, b, c
find({"name": {"$in": ["a"."b"."c"]}})
// The name does not satisfy the result of a, b, or c
find({"name": {"$nin": ["a"."b"."c"]}})
Copy the code

10. Output several

find()
.limit(10)
Copy the code

11. Output from which line

find()
.skip(10)
Copy the code

12. Ascending and descending order

/ / ascending
find()
.sort({"_id": - 1})
/ / descending
find()
.sort({"_id":1})
Copy the code

11. Output from which line

find()
.skip(10)
` `11. Output from which` `javascript
find()
.skip(10)
Copy the code

12. How many are there

find({})
.countDocuments()

// Note that count is deprecated and may be deleted in the future
find({})
.count()
Copy the code

add

insert({'name':1})
    .then(idea= >{
        // The storage succeeded
    })

new Idea(str)
    .save()
    .then(ides= >{
        // The storage succeeded
    })
Copy the code

update

1. Basic usage

// The first bracket is the condition, and the second bracket is the update
update({"age": {"$gt": 10}}, {"$set": { "title": "mongodb"}})
Copy the code
// Save ({_id: 123, title:"mongodb"})
Copy the code