mongodb增删改查对应mysql

 

这边是mongodb查询语句

这边边是sql语句。

db.users.find()

select * from users

db.users.find({"age" : 27})

select * from users where age = 27

db.users.find({"username" : "joe", "age" : 27})

select * from users where "username" = "joe" and age = 27

db.users.find({}, {"username" : 1, "email" : 1})

select username, email from users

db.users.find({"username" : "joe", "age" : 27}) .sort{"creatDate":1})

select * from users where "username" = "joe" and age = 27 order by desc

db.users.find({}, {"username" : 1, "_id" : 0})

// no case  // 即时加上了列筛选,_id也会返回;必须显式的阻止_id返回

db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})

select * from users where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=)

db.users.find({"username" : {"$ne" : "joe"}})

select * from users where username <> "joe"

db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}})

select * from users where ticket_no in (725, 542, 390)

db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}})

select * from users where ticket_no not in (725, 542, 390)

db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]})

select * form users where ticket_no = 725 or winner = true

db.users.find({"id_num" : {"$mod" : [5, 1]}})

select * from users where (id_num mod 5) = 1

db.users.find({"$not": {"age" : 27}})

select * from users where not (age = 27)

db.users.find({"username" : {"$in" : [null], "$exists" : true}}) 询,那么连带"没有username"的纪录一并筛选出来

select * from users where username is null // 如果直接通过find({"username" : null})进行查

db.users.find({"name" : /joey?/i})

// 正则查询,value是符合PCRE的表达式

db.food.find({fruit : {$all : ["apple", "banana"]}})

// 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录

db.food.find({"fruit.2" : "peach"})

// 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录

db.food.find({"fruit" : {"$size" : 3}})

// 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用

db.users.findOne(criteria, {"comments" : {"$slice" : 10}})

// 对数组的查询,只返回数组comments中的前十条,还可以{"$slice" : -10}, {"$slice" : [23, 10]}; 分别返回最后10条,和中间10条

db.people.find({"name.first" : "Joe", "name.last" : "Schmoe"})  // 嵌套查询

db.blog.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}) // 嵌套查询,仅当嵌套的元素是数组时使用,

db.foo.find({"$where" : "this.x + this.y == 10"}) // 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where

db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"}) // $where可以支持javascript函数作为查询条件

db.foo.find().sort({"x" : 1}).limit(1).skip(10); // 返回第(10, 11]条,按"x"进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number

其他语句,创建索引

db.cacheMemberAttrs_dev.getIndexes() ##查看所有索引

 

 

db.cacheMemberAttrs_dev.ensureIndex({"attrValue" :1})

##对attrValue列建立索引,options 1或者-1 按正反顺序创建

create index index_name on table_name (列名)  CREATE INDEX index_name ON table_name (column_list)

db.cacheMemberAttrs_dev.reIndex({"attrValue" :1}) #重建索引

ALTER TABLE `inv`.`inv_post` ADD INDEX `idx_id`(`id`);

db.table.ensureIndex({"name":1,"age":1}) // 联合索引

 

db.table.ensureIndex({"name":1},{"unique":true})  // 唯一索引

 

db.table.dropIndex({"name":1}) // 删除索引

 
  

1、统计表行数

    db.users.find().count();

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以回答这个问题。 Java实现增删查图书管理系统可以使用Java语言编写。首先需要设计数据库,可以使用MySQL或Oracle等关系型数据库,也可以使用NoSQL数据库如MongoDB等。接着可以使用Java的JDBC或ORM框架如Hibernate来访问数据库。编写程序时需要实现增加、删除、修、查询等功能,并考虑安全性和性能优化等方面的问题。另外,可以使用Java的GUI框架如JavaFX或Swing来设计图形界面,提高用户体验。 ### 回答2: Java实现增删查图书管理系统可以通过以下步骤进行: 1. 首先,定义一个Book类,包含图书的属性,例如书名、作者、出版社等。可以使用私有成员变量以及相的访问方法。 2. 创建一个图书管理系统类,其中包含一个ArrayList作为图书库存。可以使用增加、删除、修和查询方法对图书进行管理。 3. 在添加图书的方法中,创建一个新的Book对象,并将其添加到图书库存中。 4. 在删除图书的方法中,遍历图书库存并根据图书的某个属性(例如书名或作者)匹配后,删除对的图书对象。 5. 在修图书的方法中,同样遍历图书库存并匹配要修的图书对象,然后根据需要修图书的各个属性。 6. 在查询图书的方法中,可以根据输入的关键字(例如书名或作者)遍历图书库存并返回匹配的图书对象。 7. 可以编写一个主程序,利用图书管理系统类的各种功能进行测试。 示例代码如下: ```java import java.util.ArrayList; class Book { private String title; private String author; private String publisher; public Book(String title, String author, String publisher) { this.title = title; this.author = author; this.publisher = publisher; } public String getTitle() { return title; } public String getAuthor() { return author; } public String getPublisher() { return publisher; } } class BookManagementSystem { private ArrayList<Book> bookInventory; public BookManagementSystem() { bookInventory = new ArrayList<>(); } public void addBook(Book book) { bookInventory.add(book); } public void deleteBook(String title) { for (Book book : bookInventory) { if (book.getTitle().equals(title)) { bookInventory.remove(book); break; } } } public void modifyBook(String title, String author, String publisher) { for (Book book : bookInventory) { if (book.getTitle().equals(title)) { book.author = author; book.publisher = publisher; break; } } } public ArrayList<Book> searchBook(String keyword) { ArrayList<Book> result = new ArrayList<>(); for (Book book : bookInventory) { if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) { result.add(book); } } return result; } } public class Main { public static void main(String[] args) { BookManagementSystem bms = new BookManagementSystem(); // 添加图书 Book book1 = new Book("Java编程思想", "Bruce Eckel", "机械工业出版社"); bms.addBook(book1); // 删除图书 bms.deleteBook("Java编程思想"); // 修图书 bms.modifyBook("Java编程思想", "Bruce Eckel", "机械工业出版社"); // 查询图书 ArrayList<Book> searchResult = bms.searchBook("Java"); for (Book book : searchResult) { System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",出版社:" + book.getPublisher()); } } } ``` 以上是一个简单的Java实现的增删查图书管理系统,可以根据具体需求进行扩展和优化。 ### 回答3: Java实现增删查图书管理系统可以通过以下步骤实现: 1. 创建图书类:首先,我们需要创建一个图书类,包含图书的属性(如图书名称、作者、出版社等),以及图书的get和set方法。 2. 创建图书管理系统类:创建一个图书管理系统类,包含对图书进行增删查的方法。通过ArrayList来存储图书信息。 3. 实现增加图书:通过用户输入图书的相关信息,创建一个新的图书对象,并将该对象添加到图书列表中。 4. 实现删除图书:根据用户输入的图书名称或其他相关信息,遍历图书列表,找到匹配的图书对象,并将其从列表中移除。 5. 实现修图书:根据用户输入的图书名称或其他相关信息,遍历图书列表,找到匹配的图书对象,并根据用户输入的修信息进行更新。 6. 实现查询图书:根据用户输入的图书名称或其他相关信息,遍历图书列表,找到匹配的图书对象,并将相关信息进行展示。 7. 创建测试类:创建一个测试类,通过实例化图书管理系统对象,调用相的方法进行测试。 通过以上步骤,我们可以实现一个简单的图书管理系统。当然,对于一个完整的图书管理系统,还需要考虑到更多的功能和细节,如借阅、归还等操作,以及数据的持久化(可以使用数据库进行存储),这需要根据具体需求来进行深入开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值