重新学习mongodb:面向文档的数据

5 篇文章 0 订阅
3 篇文章 0 订阅

schema设计(概要,计划,图表):

使用数据库系统建模时,可以先思考以下问题:

电子商务数据模式:

商品文档的例子:

doc =
{ _id: new ObjectId("4c4b1476238d3b4dd5003981"),
  slug: "wheel-barrow-9092",
  sku: "9092",
  name: "Extra Large Wheel Barrow",
  description: "Heavy duty wheel barrow...",

  details: {
    weight: 47,
    weight_units: "lbs",
    model_num: 4039283402,
    manufacturer: "Acme",
    color: "Green"
  },

  total_reviews: 4,
  average_review: 4.5,

  pricing:  {
    retail: 589700,
    sale: 489700,
  },

  price_history: [
      {retail: 529700,
       sale: 429700,
       start: new Date(2010, 4, 1),
       end: new Date(2010, 4, 8)
      },

      {retail:  529700,
       sale:  529700,
       start: new Date(2010, 4, 9),
       end: new Date(2010, 4, 16)
      },
  ],

  category_ids: [new ObjectId("6a5b1476238d3b4dd5000048"),
                    new ObjectId("6a5b1476238d3b4dd5000049")],

  main_cat_id: new ObjectId("6a5b1476238d3b4dd5000048"),

  tags: ["tools", "gardening", "soil"],

}

商品类别:

doc =
{  _id: ObjectId("6a5b1476238d3b4dd5000048"),
   slug: "gardening-tools",
   ancestors: [{ name: "Home",
                 _id:  ObjectId("8b87fb1476238d3b4dd50003"),
                 slug: "home"
                },

                { name: "Outdoors",
                  _id:  ObjectId("9a9fb1476238d3b4dd500001"),
                  slug: "outdoors"
                }
   ],

   parent_id: ObjectId("9a9fb1476238d3b4dd500001"),

   name: "Gardening Tools",
   description: "Gardening gadgets galore!",
}

查找商品:

db.products.find({category_ids:ObjectId('6a5b1476238d3b4dd5000048')});

在商品目录下查询所有的类别,使用$in操作符

products = db.products.findOne({category_ids:ObjectId('6a5b1476238d3b4dd5000048')});
product = products.category_ids;
print(product);
db.categories.find({_id:{$in:product}})

用户和订单

{ _id: ObjectId("4c4b1476238d3b4dd5000001"),
  username: "kbanker",
  email: "kylebanker@gmail.com",
  first_name: "Kyle",
  last_name: "Banker",
  hashed_password: "bd1cfa194c3a603e7186780824b04419",
  addresses:  [
    {name: "home",
     street: "588 5th Street",
     city: "Brooklyn",
     state: "NY",
     zip: 11215},

    {name: "work",
     street: "1 E. 23rd Street",
     city: "New York",
     state: "NY",
     zip: 10010}
  ],
  payment_methods:  [
    {name: "VISA",
     last_four: 2127,
     crypted_number: "43f6ba1dfda6b8106dc7",
     expiration_date: new Date(2014, 4)
    }
  ]
}

评价

doc =
{ _id: new ObjectId("4c4b1476238d3b4dd5000041"),
  product_id: new ObjectId("4c4b1476238d3b4dd5003981"),
  date: new Date(2010, 5, 7),
  title: "Amazing",
  text: "Has a squeaky wheel, but still a darn good wheel barrow.",
  rating: 4,

  user_id: new ObjectId("4c4b1476238d3b4dd5000041"),
  username: "dgreenthumb",


  helpful_votes: 3,
  voter_ids: [ new ObjectId("4c4b1476238d3b4dd5000041"),
                  new ObjectId("7a4f0376238d3b4dd5000003"),
                  new ObjectId("92c21476238d3b4dd5000032")
  ]
}

数据库、集合和文档:

数据库:数据库是集合和索引的命名空间和物理组合。

管理数据可:mingodb没有显示的创建数据库的方式。

删除集合中所有的数据:prooduct.find({}).delete_many

删除集合:product.drop

删除数据库:db.dropDatabase()

数据库文件的分配:

集合:是结构或者概念相似的文档容器。

管理集合:创建集合是隐身的。只有插入文档是才创建。

db.createCollection('users')

db.createCollection("users",{size:20000})

集合重命名:

db.products.renameCollection("store_product");

盖子集合:

db.createCollection("users.action",{capped:true,size:16384,max:100})

盖子集合不容许删除单个文档,也不能增加文档的大小。

TTL集合

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值