Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB lookup data base

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Using the library database

Use libraray

View available databases and collections

Show dbs show collections

Insert data insert operation

There are two formats that define the document:

Document = ({"Type": "Book", "Title": "Definitive Guide to MongoDB 2nd ed.,The", "ISBN": "978-1-4302-5821-6", "Publisher": "Apress", "Author": ["Hows, David", "Plugge, Eelco", "Membrey, Peter", "Hawkins, Tim"]}) document = ({"Type": "Book" "Title": "Definitive Guide to MongoDB 2nd ed.,The", "ISBN": "978-1-4302-5821-6", "Publisher": "Apress", "Author": ["Hows, David", "Plugge, Eelco", "Membrey, Peter", "Hawkins, Tim"]})

Insert directly using insert:

Db.media.insert (document)

You can also insert a document without defining it:

Db.media.insert ({"Type": "CD", "Artist": "Nirvana", "Title": "Nevermind"})

Or:

Db.media.insert ({"Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "Tracklist": [{"Track": "1", "Title": "Smells Like Teen Spirit", "Length": "5:02"}, {"Track": "2", "Title": "In Bloom", "Length": "4:15"}]})

Query data find operation

Query all data:

Db.media.find ()

Query specific documents:

Db.media.find ({Artist: "Nirvana"})

View specific columns

Db.media.find ({Artist: "Nirvana"}, {Title:1})

Simply return the title field information in find using the second parameter {Title:1}. Change 1 to 0 to not display the title

Specific query (somewhat like SQL's where condition):

Db.media.find ({"Tracklist.Title": "In Bloom"})

Query the data whose Title is "In Bloom" under Tracklist

Db.media.find ({"Author": "Membrey, Peter"})

Query the documents containing "Membrey, Peter" in the array Author

Use sort, limit, skip functions

Sort:

Db.media.find () .sort ({Title:1})

Ascending order, as long as the Title:1 is changed to-1, it is in reverse order

Limit the output of n documents:

Db.media.find (). Limit (2)

Unlimited display of the first document:

Db.media.find () .skip (1)

The three functions are used together:

Ann Titile in reverse order, displaying 10 documents, skipping the first one

Db.media.find () .sort ({Title:-1}) .limit (10) .skip (1)

Set in a fixed order:

Create a fixed order collection:

Db.createCollection ("audit", {capped:true,size:204800})

This collection ensures that the inserted documents are sorted in the order in which they are inserted and will not change the order in later updates.

Aggregate function (Group by like SQL):

Count function:

Db.media.count ()

Returns how many documents there are in the current media collection

Specify the number of documents that meet the criteria:

Db.media.find ({Publisher: "Apress"}) .count ()

Note that count ignores skip and limit, for example:

Db.media.find (). Skip (1). Count () is equivalent to db.media.find (). Count ()

Do not want to ignore skip and limit. You need to add true parameters to count, for example:

Db.media.find () .skip (1) .count (true)

To repeat the distinct function:

Add a document:

Document = ({"Type": "Book", "Title": "Definitive Guide to MongoDB 2nd ed.,The", "ISBN": "978-1-4302-5821-6", "Publisher": "Apress", "Author": ["Hows, David", "Plugge, Eelco", "Membrey, Peter", "Hawkins, Tim"]}) db.media.insert (document) db.media.distinct ("Title")

In this way, Title with the same content will be merged into a single record.

Group function:

Db.media.group ({key: {Title:true}, initial: {Total:0}, reduce:function (items,prev) {prev.Total+=200;}})

Conditional query:

Add some data:

Dvd= ({"Type": "DVD", "Title": "Matrix,The", "Released": 1999, "Cast": ["Keanu Reeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano"]}) dvd= ({"Type": "DVD", Title: "Blade Runner", Released:1992}) db.media.insert (dvd) dvd= ({"Type": "DVD", Title: "Toy Story 3") Released:2010}) db.media.insert (dvd)

Greater than the operation:

Db.media.find ({Released: {$gt:2000}})

Less than:

Db.media.find ({Released: {$lt:2000}})

Gte and lte are greater than or equal to, less than or equal to

The specified range is greater than or equal to 1990 to less than or equal to 2010:

Db.media.find ({Released: {$gte:1990,$lte:2010}})

Negative query $ne:

Db.media.find ({Tyep: "Book", Author: {$ne: "Plugge,Eelco"}})

Operation of the array:

$in can be matched by one of them

Db.media.find ({Released: {$in: [1999, 2008, 2009]}})

None of the $nin matches.

Db.media.find ({Released: {$nin: [1999, 2008, 2009]}})

$all matches all:

Db.media.find ({Released: {$all: [1999, 2008, 2009]}})

The above statement does not return any data

$or multi-conditional search:

Db.media.find ({$or: [{"Title": "Toy Story 3"}, {"ISBN": "978-1-4302-5821-6"}]})

Add parameter limit type:

Db.media.find ({"Type": "DVD", $or: [{"Title": "Toy Story 3"}, {"ISBN": "978-1-4302-5821-6"}]})

$slice operation array range:

Show the first 3 items:

Db.media.find ({"Title": "Matrix,The"}, {"Cast": {$slice:3}})

The last three items:

Db.media.find ({"Title": "Matrix,The"}, {"Cast": {$slice:-3}})

Ignore the first 2 items and start with the 3 data items:

Db.media.find ({"Title": "Matrix,The"}, {"Cast": {$slice: [2p3]}})

Output 4 pieces of data from the penultimate 5:

Db.media.find ({"Title": "Matrix,The"}, {"Cast": {$slice: [- 5Power4]}})

Odd and even number operations:

Even-numbered years:

Db.media.find ({Released: {$mod: [2Jing 0]}})

Odd number:

Db.media.find ({Released: {$mod: [2Jing 1]}})

The number of elements in the query array meets a specific value:

Db.media.find ({Tracklist: {$size:2}})

A document with only two elements in the Tracklist array

Returns the existence of a specific object:

Db.media.find ({Author: {$exists:true}})

Match a complete array

Add a document:

Nirvana = ({"Type": "CD", "Artist": "Nirvana", "Title": "Nirvana", "Tracklist": [{"Track": "1", "Title": "You Know You're Right", "Length": "3:38"}, {"Track": "5", "Title": "Smells Like Teen Spirit", "Length": "5:02"}]})

Finding data with Tracklist.Title of "Smells Like Teen Spirit" and Track 1 will be duplicated by multiple lines

Use $elemMatch to match complete documents in an array

Db.media.find ({"Tracklist": {"$elemMatch": {Title: "Smells Like Teen Spirit", "Track": "1"})

You can use $not to negate this condition, for example:

Db.media.find ({"Tracklist": {$not: {"$elemMatch": {Title: "Smells Like Teen Spirit", "Track": "1"})

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report