arongsoft 发表于 2018-10-28 08:30:50

MongoDB: 3. Schema Design

  
> for (var i = 0; i < 10; i++) db.users.insert({name:&quot;user&quot;+i, age:i})
  

  
> db.users.find()
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226e&quot;), &quot;name&quot; : &quot;user0&quot;, &quot;age&quot; : 0 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226f&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 1 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2270&quot;), &quot;name&quot; : &quot;user2&quot;, &quot;age&quot; : 2 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2271&quot;), &quot;name&quot; : &quot;user3&quot;, &quot;age&quot; : 3 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2272&quot;), &quot;name&quot; : &quot;user4&quot;, &quot;age&quot; : 4 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2273&quot;), &quot;name&quot; : &quot;user5&quot;, &quot;age&quot; : 5 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2274&quot;), &quot;name&quot; : &quot;user6&quot;, &quot;age&quot; : 6 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2275&quot;), &quot;name&quot; : &quot;user7&quot;, &quot;age&quot; : 7 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2276&quot;), &quot;name&quot; : &quot;user8&quot;, &quot;age&quot; : 8 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2277&quot;), &quot;name&quot; : &quot;user9&quot;, &quot;age&quot; : 9 }
  

  
> db.users.find({$where:&quot;this.age > 7 || this.age < 3&quot;})
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226e&quot;), &quot;name&quot; : &quot;user0&quot;, &quot;age&quot; : 0 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226f&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 1 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2270&quot;), &quot;name&quot; : &quot;user2&quot;, &quot;age&quot; : 2 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2276&quot;), &quot;name&quot; : &quot;user8&quot;, &quot;age&quot; : 8 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2277&quot;), &quot;name&quot; : &quot;user9&quot;, &quot;age&quot; : 9 }
  

  
> db.users.find(&quot;this.age > 7 || this.age < 3&quot;)
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226e&quot;), &quot;name&quot; : &quot;user0&quot;, &quot;age&quot; : 0 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226f&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 1 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2270&quot;), &quot;name&quot; : &quot;user2&quot;, &quot;age&quot; : 2 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2276&quot;), &quot;name&quot; : &quot;user8&quot;, &quot;age&quot; : 8 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2277&quot;), &quot;name&quot; : &quot;user9&quot;, &quot;age&quot; : 9 }
  

  
> db.users.find({$where: function(){ return this.age > 7 || this.age < 3;}})
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226e&quot;), &quot;name&quot; : &quot;user0&quot;, &quot;age&quot; : 0 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da226f&quot;), &quot;name&quot; : &quot;user1&quot;, &quot;age&quot; : 1 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2270&quot;), &quot;name&quot; : &quot;user2&quot;, &quot;age&quot; : 2 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2276&quot;), &quot;name&quot; : &quot;user8&quot;, &quot;age&quot; : 8 }
  
{ &quot;_id&quot; : ObjectId(&quot;4c47b3372a9b2be866da2277&quot;), &quot;name&quot; : &quot;user9&quot;, &quot;age&quot; : 9 }
  

  
>>> for u in db.users.find({&quot;$where&quot;:&quot;this.age > 7 || this.age < 3&quot;}): print u
  
...
  
{u'age': 0.0, u'_id': ObjectId('4c47b3372a9b2be866da226e'), u'name': u'user0'}
  
{u'age': 1.0, u'_id': ObjectId('4c47b3372a9b2be866da226f'), u'name': u'user1'}
  
{u'age': 2.0, u'_id': ObjectId('4c47b3372a9b2be866da2270'), u'name': u'user2'}
  
{u'age': 8.0, u'_id': ObjectId('4c47b3372a9b2be866da2276'), u'name': u'user8'}
  
{u'age': 9.0, u'_id': ObjectId('4c47b3372a9b2be866da2277'), u'name': u'user9'}
  

  
>>> for u in db.users.find().where(&quot;this.age > 7 || this.age < 3&quot;): print u
  
...
  
{u'age': 0.0, u'_id': ObjectId('4c47b3372a9b2be866da226e'), u'name': u'user0'}
  
{u'age': 1.0, u'_id': ObjectId('4c47b3372a9b2be866da226f'), u'name': u'user1'}
  
{u'age': 2.0, u'_id': ObjectId('4c47b3372a9b2be866da2270'), u'name': u'user2'}
  
{u'age': 8.0, u'_id': ObjectId('4c47b3372a9b2be866da2276'), u'name': u'user8'}
  
{u'age': 9.0, u'_id': ObjectId('4c47b3372a9b2be866da2277'), u'name': u'user9'}
  

  
>>> for u in db.users.find().where(&quot;function() { return this.age > 7 || this.age < 3;}&quot;): print u
  
...
  
{u'age': 0.0, u'_id': ObjectId('4c47b3372a9b2be866da226e'), u'name': u'user0'}
  
{u'age': 1.0, u'_id': ObjectId('4c47b3372a9b2be866da226f'), u'name': u'user1'}
  
{u'age': 2.0, u'_id': ObjectId('4c47b3372a9b2be866da2270'), u'name': u'user2'}
  
{u'age': 8.0, u'_id': ObjectId('4c47b3372a9b2be866da2276'), u'name': u'user8'}
  
{u'age': 9.0, u'_id': ObjectId('4c47b3372a9b2be866da2277'), u'name': u'user9'}


页: [1]
查看完整版本: MongoDB: 3. Schema Design