zoukankan      html  css  js  c++  java
  • MongoDB增删查改

    1.insert

    db.Customers.insert({
      "DateTest":new Date(),
      "IntTest":32,
      "DoubleTest":3.1415926,
      "StringTest":"Test",
      "BoolTest":true,
      "ArryTest":["a", "b", "c"],
      "aaa":undefined, 
      "info" : { "x" : 203 ,"y" : 102} 
    });

    2.find

    <1>"$gt", "$gte", "$lt", "$lte", "$ne"
    --where IntTest>30
    db.Customers.find({ "IntTest" : { "$gt" : 30 } })
    --where IntTest<30
    db.Customers.find({ "IntTest" : { "$lt" : 30 } })
    --where IntTest!=1
    db.Customers.find({ "IntTest" : { "$ne" : 1 } })
    --where IntTest==30
    db.Customers.find({ "IntTest" : 30 })
    <2>"$or", "$in","$nin"
    --where FirstName="Bobr"
    db.Customers.find({ "FirstName" : "Bobr" })
    --where FirstName="Bobr" or FirstName="Bobr1"
    db.Customers.find({ "$or" : [{ "FirstName" : "Bobr" }, { "FirstName" : "Bobr1" }] })
    --where FirstName="Bobr" and LastName="Dillon"
    db.Customers.find({ "FirstName" : "Bobr", "LastName" : "Dillon" })
    --where FirstName in ("Bobr","Bobr1")
    db.Customers.find({ "FirstName" : { "$in" : ["Bobr", "Bobr1"] } })
    --where FirstName not in ("Bobr","Bobr1") 没有"FirstName"的也会找出来
    db.Customers.find({ "FirstName" : { "$nin" : ["Bobr", "Bobr1"] } })

    <3>正则表达式
    --FirstName K开头
    db.Customers.find({ "FirstName" : /^K/ })
    --FirstName like '%Bob%'
    db.Customers.find({ "FirstName" : /Bob/ })
    <4>$where
    db.Customers.find({ "$where" : "this.FirstName == 'KBobr'" })

    3.Update $inc(没有就新增,有就在原有基础上添加) 和 $set
    db.Customers.update({ "FirstName" : /Bob/, "$atomic" : "true" },{$inc:{"age":500}}, false, true)
    db.Customers.update({ "FirstName" : /Bob/, "$atomic" : "true" },{$set:{"age":500}}, false, true)

    4.Remove
    db.Customers.remove({ "_id" : ObjectId("567b55a50e906e261435dafb") }, $atomic: true);
    db.Customers.remove({ "FirstName" : "KBobr" }, $atomic: true);

  • 相关阅读:
    【Hadoop】MapReduce练习:多job关联实现倒排索引
    【Hadoop】MapReduce练习:分科目等级并按分区统计学生以及人数
    【Zookeeper】利用zookeeper搭建Hdoop HA高可用
    【Linux】部署NTP时间同步服务器
    VSCode前端文件以服务器模式打开
    移动端公共方法封装
    常用浏览器及内核
    XHTML和HTML的区别
    javascript算法
    计算属性和侦听器
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/5114114.html
Copyright © 2011-2022 走看看