MongoDB
 sql >> база данни >  >> NoSQL >> MongoDB

Вложени обекти в схеми на мангусти

const mongoose = require("mongoose");

// Make connection
// https://mongoosejs.com/docs/connections.html#error-handling
mongoose.connect("mongodb://localhost:27017/test", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

// Define schema
// https://mongoosejs.com/docs/models.html#compiling
const AddressSchema = mongoose.Schema({
  city: String,
  street: String,
  houseNumber: String,
});

const ContactInfoSchema = mongoose.Schema({
  tel: [Number],
  email: [String],
  address: {
    type: AddressSchema,
    required: true,
  },
});

const CustomerSchema = mongoose.Schema({
  firstName: String,
  lastName: String,
  company: String,
  connectInfo: ContactInfoSchema,
});

const CustomerModel = mongoose.model("Customer", CustomerSchema);

// Create a record
// https://mongoosejs.com/docs/models.html#constructing-documents
const customer = new CustomerModel({
  firstName: "Ashish",
  lastName: "Suthar",
  company: "BitOrbits",
  connectInfo: {
    tel: [8154080079, 6354492692],
    email: ["[email protected]", "[email protected]"],
  },
});

// Insert customer object
// https://mongoosejs.com/docs/api.html#model_Model-save
customer.save((err, cust) => {
  if (err) return console.error(err);

  // This will print inserted record from database
  // console.log(cust);
});

// Display any data from CustomerModel
// https://mongoosejs.com/docs/api.html#model_Model.findOne
CustomerModel.findOne({ firstName: "Ashish" }, (err, cust) => {
  if (err) return console.error(err);

  // To print stored data
  console.log(cust.connectInfo.tel[0]); // output 8154080079
});

// Update inner record
// https://mongoosejs.com/docs/api.html#model_Model.update
CustomerModel.updateOne(
  { firstName: "Ashish" },
  {
    $set: {
      "connectInfo.tel.0": 8154099999,
    },
  }
);


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Групиране по условие в MongoDB

  2. mongorestore неуспешно:няма достъпни сървъри

  3. Не може да се свърже с mongodb errno:61 Връзката е отказана

  4. как да изтеглите файл, записан в gridFS, с помощта на nodeJS

  5. sql заявка към mongodb?