Giter Site home page Giter Site logo

onee's People

Contributors

class4cxy avatar kevin225 avatar

Watchers

 avatar  avatar

onee's Issues

Mongodb 增、删、查、改

1.Mongodb 操作语句列表

1.1查询操作 - db.collection.find

for example

db.users.find(
  {age : {$gt : 18}}, // 查询条件
  {name : 1, address : 1} // 输出字段
).limit(5)

等同SQL

SELECT _id, name, address
FROM users
WHERE age>18
LIMIT 5

1.2插入操作 - db.collection.insert

for example

db.users.insert({
  name : "chen",
  age : "26",
  status : "A"
})

等同SQL

INSERT INTO users
(name, age, status)
VALUE
("chen", "26", "A")

[注] 默认行为:如果增加的文档不包含_id字段,系统会默认增加该字段,如果包含_id字段,需要保证取值在当前集合[collection]的唯一性;若重复,插入不成功并返回错误

1.3更新操作 - db.collection.update|db.collection.save

for example

db.users.update(
  {age : {$gt : 18}}, // 查询条件
  {$set : {status : 'B'}} // 更新字段
  {multi : true} // 更新选项[默认为更新所有匹配文档,`1` or `true`仅更新第一条]
)

等同SQL

UPDATE users
SET status='B'
WHERE age>18

[注] save方法仅仅对指定_id的文档进行替换,update方法允许查询语句,指定字段修改,修改选项。

1.4删除操作 - db.collection.remove

for example

db.users.remove(
  {age : {$gt : 18}}, // 查询条件
  1 //删除选项[默认为删除所有匹配文档,`1` or `true`仅删除第一条]
)

等同SQL

DELETE FROM users
WHERE age>18

2.查询条件语法

[注] 各操作语句中使用的查询条件语法一致,以下例子使用find方法代替

2.1普通文档

// 查找集合中的所有文档
db.users.find({})
or
db.users.find()
// 指定相等条件,where type='snacks'
db.users.find({type : "snacks"})
// 使用查询操作符指定条件,where type='food' or type='snacks'
db.users.find(
  {type : {$in: [ 'food', 'snacks' ]}}
)
// 使用`与`条件,where type='food' and price
// 使用`或`条件,where qty>100 or price
// 同时使用`与`、`或` where qty>100 or price

2.2嵌套类型文档

// 精准匹配内嵌文档,匹配集合中内嵌文档`producer`含有且仅含有company='ABC123'与address='123 Street'字段
db.users.find({
  producer : {
    company : 'ABC123',
    address : '123 Street'
  }
})
// 内嵌文档中的相等字段匹配,使用`.`连接符可以针对内嵌文档内部某一字段进行匹配
// [注]key必须加引号
db.users.find({'producer.company' : 'ABC123'})

2.3数组类型文档

// 精准匹配数组类型文档,匹配tags=['fruit', 'food', 'citrus']
// [注]相等包括数组元素相同,排序相同
db.users.find({tags : ['fruit', 'food', 'citrus']})
// 匹配数组某一元素,匹配字段为tags的数组包含'fruit'元素
// [注]该语句同样可以匹配普通文档类型字段tags='fruit'
db.inventory.find({tags : 'fruit'})
// 匹配数组中指定元素,匹配字段为tags的数组第一个元素为'fruit'
// [注]key必须加引号
db.inventory.find({'tags.0' : 'fruit'})

2.4数组中的嵌套文档

// 通过数据索引[index]匹配数组中某元素的某字段,匹配字段为tags的数组第一个元素的'by'字段为'shipping'
// [注]key必须加引号
db.inventory.find({'memos.0.by' : 'shipping'})
// 匹配数组中所有元素的某字段,匹配字段为tags的数组中元素包含字段'by'为'shipping'
// [注]key必须加引号,该语句同样可以匹配内嵌文档`memos`中字段`by`为`shipping`
db.inventory.find({'memos.by' : 'shipping'})
// 匹配数组中所有元素的多个字段
// [注]同样可以作为内嵌文档`memos`的多字段匹配
db.inventory.find(
  {
    'memos.memo' : 'on time',
    'memos.by' : 'shipping'
  }
)

让node进程在后台可靠运行的几种方案

原因 通常情况,node是通过终端指令来启动,当用户注销或者网络断开之后,终端会接收到一个叫HUP(handup)的信号,从而关闭了当前终端的所有子进程。

思路 要么让node进程忽略HUP信号;要么让node进程运行在新的会话中,从而不属于当前终端的子进程。

方案

忽略HUP信号:nohup指令,让提交的命令忽略 hangup 信号

// 执行
[root@ node]# nohup node main.js & //`&`表示进程后台运行,可以通过jobs执行查看
[1] 19943
[root@ node]# nohup: 忽略输入并把输出追加到"nohup.out"
// 检查
[root@ node]# ps -ef|grep node
root     19943 18940  0 11:46 pts/1    00:00:00 node main.js
root     20207 18940  0 11:54 pts/1    00:00:00 grep node

让node运行在新的进程中:setsid指令,让提交的命令运行在新的 session 中;() &配合同样也可以

// 执行
[root@ node]# setsid node main.js
[root@ node]#
// 检查
[root@ node]# ps -ef|grep node
root      1255     1  0 12:06 ?        00:00:00 node main.js
root      1269  1135  0 12:07 pts/0    00:00:00 grep node
// 执行
[root@ node]# (node main.js &)
[root@ node]#
// 检查
[root@ node]# ps -ef|grep node
root      1255     1  0 12:06 pts/1    00:00:00 node main.js
root      1269  1135  0 12:07 pts/0    00:00:00 grep node

推荐:常见做法还可以用forever模块,forever除了解决node后台运行,还提供了更丰富功能,详情:https://github.com/nodejitsu/forever

[root@ node]# npm install forever -g // 安装
[root@ node]# forever start main.js // 运行
[root@ node]# forever stop main.js // 停止
[root@ node]# forever start -l forever.log -o out.log -e err.log app.js // 带参数(日志、错误输出)运行

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.